Skip to contentSkip to conversation

MMIO address changes

Qemu-devel In review Paul Brook 6 participants 18 messages Dec 1, 2008 – Jan 7, 2009

lore
Share & tools
lore

Paul Brook

author
#0 lore raw

I've just committed a patch that changes the MMIO callback interface for devices. Instead of being passed an absolute address these are now passed an offset from the start[1] of the memory region that was registered.

By itself this change has fairly neutral benefit, it just moves logic about. However it makes subsequent dynamic board configuration bits nicer, and is a step towards a proper bus level API.

Most of the groundwork for this is already there, from my earlier changes to separate ram and MMIO addresses TLB handling.

The main notable change it that the PhysPageDesc structure is not bigger. This isn't ideal, however l2_phys_map needs to go away anyway, so I'm not really worried about this.

Some devices register their memory regions in multiple segments. To facilitate this I have added cpu_register_physical_memory_offset.

Most of the remaining changes are fairly mechanical tweaks to fix devices that explicitly compensated for the absolute address. Many devices are untouched because they ignore the high bits of the address.

I've tried to be fairly thorough with the changes, and tested what I can. However it's possible I missed or broke something, so please test your favourite targets.

Paul

[1] It's actually the offset from the start of the first page of that region. In practice this difference doesn't matter, and makes the implementation a lot simpler.

#1 lore raw

Hi,

Quoted 3 lines

I've just committed a patch that changes the MMIO callback interface for devices. Instead of being passed an absolute address these are now passed an offset from the start[1] of the memory region that was registered.

Good! It is just what I wanted.

Quoted 3 lines

I've tried to be fairly thorough with the changes, and tested what I can. However it's possible I missed or broke something, so please test your favourite targets.

Mmmmm, sh/r2d is broken. I'll post a fix later.
But, before that, could you please tell me if this modification below
is good and acceptable ?

exec.c +0 -0
@@ -2295,6 +2295,7 @@ void cpu_register_physical_memory_offset(target_phys_addr_

t start_addr,

                 p->region_offset = 0;
             } else {
                 p->phys_offset = phys_offset;
+                p->region_offset = region_offset;
                 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
                     (phys_offset & IO_MEM_ROMD))
                     phys_offset += TARGET_PAGE_SIZE;

You can see the difference when you register a region overriding
other(perhaps bigger) region.

hw/sh7750.c hits this by registering
0xfc000000 .. 0xffffffff for CPU control registers,
then
0xffe00000 .. 0xffe00028
0xffe80000 .. 0xffe80028
0xffd00000 .. 0xffd01000
0xffd80000 .. 0xffd81000
...
for peripheral modules.

I can divide the first region to some pieces to avoid overlapping. That is one solution, and is not bad to do (even without this issue), though. Ah, in addition,
I'd like to know if registering overlapping region is valid or no.

Quoted 3 lines

[1] It's actually the offset from the start of the first page of that region. In practice this difference doesn't matter, and makes the implementation a lot simpler.

Simpler is better.
But, I'd like to have a guide to write a code when it does matter.

Now, at least hw/sm501.c has been already broken by this case. It registers palette register array at
base + MMIO_BASE_OFFSET + SM501_DC + SM501_DC_PANEL_PALETTE, = 0x10000000 + 0x3e00000 + 0x080000 + 0x400
so, palette index offsets 0x400.

Of cource I can fix it by
addr -= (MMIO_BASE_OFFSET + SM501_DC + SM501_DC_PANEL_PALETTE)&~TARGET_PAGE_MASK;
before accessing the register. Looks not so smart...
Are there any idea?

Cheers,
/yoshii

author
#2 lore raw
Quoted 11 lines
exec.c +1 -0
@@ -2295,6 +2295,7 @@ void
 cpu_register_physical_memory_offset(target_phys_addr_ t start_addr,
                 p->region_offset = 0;
             } else {
                 p->phys_offset = phys_offset;
+                p->region_offset = region_offset;
                 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
                     (phys_offset & IO_MEM_ROMD))
Quoted 1 line
                     phys_offset += TARGET_PAGE_SIZE;

I guess this makes sense, however..

Quoted 11 lines

You can see the difference when you register a region overriding
other(perhaps bigger) region.

hw/sh7750.c hits this by registering
0xfc000000 .. 0xffffffff for CPU control registers,
then
0xffe00000 .. 0xffe00028
0xffe80000 .. 0xffe80028
0xffd00000 .. 0xffd01000
0xffd80000 .. 0xffd81000
...

You should not rely on overlapping regions doing anything sensible.

Paul

author
#3 lore raw
In reply to [email protected]
Quoted 11 lines

Now, at least hw/sm501.c has been already broken by this case. It registers palette register array at
base + MMIO_BASE_OFFSET + SM501_DC + SM501_DC_PANEL_PALETTE, = 0x10000000 + 0x3e00000 + 0x080000 + 0x400
so, palette index offsets 0x400.

Of cource I can fix it by
addr -= (MMIO_BASE_OFFSET + SM501_DC +
SM501_DC_PANEL_PALETTE)&~TARGET_PAGE_MASK; before accessing the register. Looks not so smart...
Are there any idea?

Proably simpler to just use a single region for both.

Paul

#7 lore raw
In reply to Paul Brook
Quoted 18 lines · Paul Brook

I've just committed a patch that changes the MMIO callback interface for devices. Instead of being passed an absolute address these are now passed an offset from the start[1] of the memory region that was registered.

By itself this change has fairly neutral benefit, it just moves logic about. However it makes subsequent dynamic board configuration bits nicer, and is a step towards a proper bus level API.

Most of the groundwork for this is already there, from my earlier changes to separate ram and MMIO addresses TLB handling.

The main notable change it that the PhysPageDesc structure is not bigger. This isn't ideal, however l2_phys_map needs to go away anyway, so I'm not really worried about this.

Some devices register their memory regions in multiple segments. To facilitate this I have added cpu_register_physical_memory_offset.

Quoted 7 lines

Most of the remaining changes are fairly mechanical tweaks to fix devices that explicitly compensated for the absolute address. Many devices are untouched because they ignore the high bits of the address.

I've tried to be fairly thorough with the changes, and tested what I can. However it's possible I missed or broke something, so please test your favourite targets.

FWIW the etrax-fs machine works OK although I am seeing a noticeable slow-down after the patch.

Cheers

Quoted 7 lines

Paul

[1] It's actually the offset from the start of the first page of that region. In practice this difference doesn't matter, and makes the implementation a lot simpler.

author
#8 lore raw
Quoted 6 lines
Quoted 3 lines

I've tried to be fairly thorough with the changes, and tested what I can. However it's possible I missed or broke something, so please test your favourite targets.

FWIW the etrax-fs machine works OK although I am seeing a noticeable slow-down after the patch.

Strange. I wouldn't expect significant slowdown.
AFAIK the hot-path code should be pretty much unchanged (if anything faster because the IO handlers don't need to adjust the address). Even the TLB fill handler overhead (tlb_set_page_exec) should be negligible.

Paul

#4 lore raw

PATCH 1/1 sh4: Followup to commit #5849 "Change MMIO callbacks..."

In reply to Paul Brook

Thank you, Paul.

Quoted 1 line

You should not rely on overlapping regions doing anything sensible.

OK. fixed.

Quoted 1 line

Proably simpler to just use a single region for both.

Ah, that's right. Now the code is very simple (than I have expected).

Here is the patch. Please apply it.

Fixes to be needed for commit #5849 "Change MMIO callbacks..."
hw/sh7750.c:
- Divide region of CPU control registers to avoid overlapping to peripheral modules.
- Delete unused var "icr", which had moved to hw/sh_intc.c.
hw/sm501.c:
- Merge non page aligned palette registers into the region of control registers.

---
 hw/sh7750.c |   14 ++++------
 hw/sm501.c  |   82 +++++++++++++++++++++++++---------------------------------
 2 files changed, 41 insertions(+), 55 deletions(-)
hw/sh7750.c +6 -8
index 1d18010..f44e522 100644
@@ -58,7 +58,6 @@ typedef struct SH7750State {
     uint16_t periph_portdirb;	/* Direction seen from the peripherals */
     sh7750_io_device *devices[NB_DEVICES];	/* External peripherals */
 
-    uint16_t icr;
     /* Cache */
     uint32_t ccr;
 
@@ -218,8 +217,6 @@ static uint32_t sh7750_mem_readw(void *opaque, target_phys_addr_t addr)
 	return porta_lines(s);
     case SH7750_PDTRB_A7:
 	return portb_lines(s);
-    case 0x1fd00000:
-        return s->icr;
     default:
 	error_access("word read", addr);
 	assert(0);
@@ -313,9 +310,6 @@ static void sh7750_mem_writew(void *opaque, target_phys_addr_t addr,
 	    assert(0);
 	}
 	return;
-    case 0x1fd00000:
-        s->icr = mem_value;
-	return;
     default:
 	error_access("word write", addr);
 	assert(0);
@@ -643,8 +637,12 @@ SH7750State *sh7750_init(CPUSH4State * cpu)
     sh7750_io_memory = cpu_register_io_memory(0,
 					      sh7750_mem_read,
 					      sh7750_mem_write, s);
-    cpu_register_physical_memory_offset(0x1c000000, 0x04000000,
-                                        sh7750_io_memory, 0x1c000000);
+    cpu_register_physical_memory_offset(0x1f000000, 0x1000,
+                                        sh7750_io_memory, 0x1f000000);
+    cpu_register_physical_memory_offset(0x1f800000, 0x1000,
+                                        sh7750_io_memory, 0x1f800000);
+    cpu_register_physical_memory_offset(0x1fc00000, 0x1000,
+                                        sh7750_io_memory, 0x1fc00000);
 
     sh7750_mm_cache_and_tlb = cpu_register_io_memory(0,
 						     sh7750_mmct_read,
hw/sm501.c +35 -47

118 lines not on this page · raw message

Signature · 1 line
1.5.6.3
#11 lore raw

PATCH 1/1 usb-ohci: Add address masking.

In reply to Paul Brook

Hi,
About this

Quoted 3 lines

[1] It's actually the offset from the start of the first page of that region. In practice this difference doesn't matter, and makes the implementation a lot simpler.

again.

The patch below adds address mask(0xff) to mmio read/write funcs in usb-ohci. I know now we are deleting this kind of address mask/subtract things. But because of the restriction [1] above, we have to add address mask here.

The mmio change potentially breaks PCI device emulations which has mmio region smaller than page size, like usb-ohci and rtl8139 (both have 256byte), because PCI memory regions are configured to be aligned to it size, but to page.

usb-ohci is really broken now.
rtl8139 is OK because it already has address mask. Others are not checked.

I think the most important thing is eliminating dependency to base address, and small numbers of masks should be acceptable.

Regards,
/yoshii

This patch adds address mask to mmio read/write function. Because its memory region is not always aligned to page.

---
 hw/usb-ohci.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/usb-ohci.c +2 -0
index 585f3a4..bfa7d77 100644
@@ -1366,6 +1366,7 @@ static uint32_t ohci_mem_read(void *ptr, target_phys_addr_t addr)
         fprintf(stderr, "usb-ohci: Mis-aligned read\n");
         return 0xffffffff;
     }
+    addr &= 0xff;
 
     if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
         /* HcRhPortStatus */
@@ -1462,6 +1463,7 @@ static void ohci_mem_write(void *ptr, target_phys_addr_t addr, uint32_t val)
         fprintf(stderr, "usb-ohci: Mis-aligned write\n");
         return;
     }
+    addr &= 0xff;
 
     if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
         /* HcRhPortStatus */
Signature · 1 line
1.5.6.3
#12 lore raw

Re: [Qemu-devel] [PATCH] usb-ohci: Add address masking.

2008/12/5 <[email protected]>:

Quoted 7 lines

Hi,
About this

Quoted 3 lines

[1] It's actually the offset from the start of the first page of that region. In practice this difference doesn't matter, and makes the implementation a lot simpler.

again.

Quoted 7 lines

The patch below adds address mask(0xff) to mmio read/write funcs in usb-ohci. I know now we are deleting this kind of address mask/subtract things. But because of the restriction [1] above, we have to add address mask here.

The mmio change potentially breaks PCI device emulations which has mmio region smaller than page size, like usb-ohci and rtl8139 (both have 256byte), because PCI memory regions are configured to be aligned to it size, but to page.

I think it's more correct to substract an offset from the address because masking causes the address to wrap and effectively the registers become visible at multiple addresses.

On the other hand I don't see why we need the restriction [1]. Looking at exec.c, at least for io regions, it should be acceptable for ->region_offset to be unaligned.

Quoted 4 lines

usb-ohci is really broken now.
rtl8139 is OK because it already has address mask. Others are not checked.

Cheers

#9 lore raw
In reply to Paul Brook
Quoted 11 lines · Paul Brook
Quoted 6 lines
Quoted 3 lines

I've tried to be fairly thorough with the changes, and tested what I can. However it's possible I missed or broke something, so please test your favourite targets.

FWIW the etrax-fs machine works OK although I am seeing a noticeable slow-down after the patch.

Strange. I wouldn't expect significant slowdown.
AFAIK the hot-path code should be pretty much unchanged (if anything faster because the IO handlers don't need to adjust the address). Even the TLB fill handler overhead (tlb_set_page_exec) should be negligible.

Hello Paul,

I went through your patch and AFAICT it looks fine. The only thing I saw that may cause slowdowns was the size increase of PageDesc and subpage.

In case anybody want's to reproduce this, just start the etrax image I've posted before, login trough telnet and do a ls -alR /, you'll see a very noticeable difference.

Best regards
Edgar

#14 lore raw

Re: [Qemu-devel] [PATCH] usb-ohci: Add address masking.

In reply to [email protected]

Hi,

Why are you writing "usb-ohci is *really* broken now." ?? Is it supposed to be working or not ?
On which status are you basing your affirmation?

Is it a good thing to completely break USB ohci in the development trunk?

Regards

Thomas

Quoted 66 lines · On Fri, Dec 5, 2008 at 2:52 PM, <[email protected]> wrote:

Hi,
About this

Quoted 1 line

[1] It's actually the offset from the start of the first page of that

region.

Quoted 1 line

In practice this difference doesn't matter, and makes the implementation

a

Quoted 1 line

lot simpler.

again.

The patch below adds address mask(0xff) to mmio read/write funcs in usb-ohci.
I know now we are deleting this kind of address mask/subtract things. But because of the restriction [1] above, we have to add address mask here.

The mmio change potentially breaks PCI device emulations which has mmio region
smaller than page size, like usb-ohci and rtl8139 (both have 256byte), because
PCI memory regions are configured to be aligned to it size, but to page.

usb-ohci is really broken now.
rtl8139 is OK because it already has address mask. Others are not checked.

I think the most important thing is eliminating dependency to base address, and small numbers of masks should be acceptable.

Regards,
/yoshii

This patch adds address mask to mmio read/write function. Because its memory region is not always aligned to page.

---
 hw/usb-ohci.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/usb-ohci.c +1 -0
index 585f3a4..bfa7d77 100644
@@ -1366,6 +1366,7 @@ static uint32_t ohci_mem_read(void *ptr,
 target_phys_addr_t addr)
        fprintf(stderr, "usb-ohci: Mis-aligned read\n");
        return 0xffffffff;
    }
+    addr &= 0xff;
 
    if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
        /* HcRhPortStatus */
hw/usb-ohci.c +1 -0
@@ -1462,6 +1463,7 @@ static void ohci_mem_write(void *ptr,
 target_phys_addr_t addr, uint32_t val)
        fprintf(stderr, "usb-ohci: Mis-aligned write\n");
        return;
    }
+    addr &= 0xff;
 
    if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
        /* HcRhPortStatus */

--
1.5.6.3

author
#13 lore raw

Re: [Qemu-devel] [PATCH] usb-ohci: Add address masking.

In reply to andrzej zaborowski
Quoted 3 lines

On the other hand I don't see why we need the restriction [1]. Looking at exec.c, at least for io regions, it should be acceptable for ->region_offset to be unaligned.

The low bits of env->iotlb are used for other purposes.

Paul

#15 lore raw

Re: [Qemu-devel] [PATCH] usb-ohci: Add address masking.

In reply to Thomas Bandelier

Ah, sorry, this question might be because of my bad choice of word.... # Which word was better, may be "actually" ?

Quoted 2 lines

Why are you writing "usb-ohci is *really* broken now." ?? Is it supposed to be working or not ?

It doesn't work when the memory region is not aligned to page. Otherwise works.

What I wanted to say by "really broken" was not that it was "broken beyond fix", but was
- Logically, the mmio change has a possibility to make usb-ohci not work.
- You can produce this condition on current code.
(this part became the word "really")

r2d was the case(, though it was off-tree at that time). hw/ppc* s are the same, because they initialize NICs first then usb-ohci.
qemu -nic model,rtl8139 -usb ...
can produce the case.

I wrote it "potentially breaks" because
- r2d + linux was obviously in trouble, but it was off-tree code at that time.
- I didn't have evaluation environment ready for ppc. I checked them with only qemu-ppc without software on it.
- Whether it results in a problem or not depends on the software run on it, because PCI configuration is software task.

Quoted 1 line

Is it a good thing to completely break USB ohci in the development trunk?

No. This issue can be fixed by my patch (if masking is acceptable). /yoshii

#5 lore raw

[Qemu-devel] Re: [PATCH] sh4: Followup to commit #5849 "Change MMIO callbacks..."

In reply to [email protected]
Quoted 8 lines · [email protected] wrote:

Thank you, Paul.

Quoted 1 line

You should not rely on overlapping regions doing anything sensible.

OK. fixed.

Quoted 1 line

Proably simpler to just use a single region for both.

Ah, that's right. Now the code is very simple (than I have expected).

Quoted 7 lines

Here is the patch. Please apply it.

Fixes to be needed for commit #5849 "Change MMIO callbacks..."
hw/sh7750.c:
- Divide region of CPU control registers to avoid overlapping to peripheral modules.
- Delete unused var "icr", which had moved to hw/sh_intc.c.

Does sh_intc.c actually handle icr right now? Although I have a local patch that makes it handle icr0 as well as LVLMODE, I don't see this patch in SVN HEAD of qemu.

- Volodya

#16 lore raw
In reply to Paul Brook
Quoted 3 lines · Paul Brook

I've just committed a patch that changes the MMIO callback interface for devices. Instead of being passed an absolute address these are now passed an offset from the start[1] of the memory region that was registered.

...

Quoted 3 lines

[1] It's actually the offset from the start of the first page of that region. In practice this difference doesn't matter, and makes the implementation a lot simpler.

This breaks something for me. I have:

         smc91c111_init(&nd_table[0], (0xb4000000 + 0x01800000 + 0x300) & 0x1fffffff,
                   get_irq(s, 7));

and smc91c111_init presently does:

        cpu_register_physical_memory(base, 16, iomemtype);

and smc emulation promptly dies because an access with address of 0x30e is made, with 0x300 apparently being the offset from the page start, with 0xe being the desired offset. What is the cleanest way to address this? Reverting your change to sms91c111.c works, fwiw.

- Volodya

#6 lore raw

Re: [Qemu-devel] Re: [PATCH] sh4: Followup to commit #5849 "Change MMIO callbacks..."

In reply to Vladimir Prus
Quoted 3 lines

Does sh_intc.c actually handle icr right now? Although I have a local patch that makes it handle icr0 as well as LVLMODE, I don't see this patch in SVN HEAD of qemu.

Oops, I should have sent one more.
(I found your patch includes equivalent one, later). Sorry for my careless behavior.
/yoshii

#17 lore raw
In reply to Vladimir Prus

2008/12/11 Vladimir Prus <[email protected]>:

Quoted 15 lines
Quoted 3 lines · Paul Brook

I've just committed a patch that changes the MMIO callback interface for devices. Instead of being passed an absolute address these are now passed an offset from the start[1] of the memory region that was registered.

...

Quoted 3 lines

[1] It's actually the offset from the start of the first page of that region. In practice this difference doesn't matter, and makes the implementation a lot simpler.

This breaks something for me. I have:

        smc91c111_init(&nd_table[0], (0xb4000000 + 0x01800000 + 0x300) & 0x1fffffff,
                  get_irq(s, 7));
Quoted 8 lines

and smc91c111_init presently does:

       cpu_register_physical_memory(base, 16, iomemtype);

and smc emulation promptly dies because an access with address of 0x30e is made, with 0x300 apparently being the offset from the page start, with 0xe being the desired offset. What is the cleanest way to address this? Reverting your change to sms91c111.c works, fwiw.

Masking the address with 15 should be acceptable for smc91c111.c I think. The other possibility is reverting the change to keep subtracting the base address, but which would be masked with TARGET_PAGE_MASK.

Cheers

#10 lore raw
In reply to Edgar E. Iglesias
Quoted 23 lines · Edgar E. Iglesias
Quoted 22 lines · Paul Brook

I've just committed a patch that changes the MMIO callback interface for devices. Instead of being passed an absolute address these are now passed an offset from the start[1] of the memory region that was registered.

By itself this change has fairly neutral benefit, it just moves logic about. However it makes subsequent dynamic board configuration bits nicer, and is a step towards a proper bus level API.

Most of the groundwork for this is already there, from my earlier changes to separate ram and MMIO addresses TLB handling.

The main notable change it that the PhysPageDesc structure is not bigger. This isn't ideal, however l2_phys_map needs to go away anyway, so I'm not really worried about this.

Some devices register their memory regions in multiple segments. To facilitate this I have added cpu_register_physical_memory_offset.

Most of the remaining changes are fairly mechanical tweaks to fix devices that explicitly compensated for the absolute address. Many devices are untouched because they ignore the high bits of the address.

Quoted 6 lines
Quoted 3 lines

I've tried to be fairly thorough with the changes, and tested what I can. However it's possible I missed or broke something, so please test your favourite targets.

FWIW the etrax-fs machine works OK although I am seeing a noticeable slow-down after the patch.

I tracked this down and the MMIO patch was for some reason triggering a bug in the ETRAX models causing the slowdown. ETRAX ethernet/dma works fine again.

Cheers