Skip to contentSkip to conversation

Sun4c interrupt controller, MMU, IOMMU?

DISCUSSION Blue Swirl 3 participants 10 messages Aug 12, 2007 – Aug 13, 2007

lore
Share & tools
lore

Blue Swirl

author
#0 lore raw

Hi,

I'm trying to add Sun4c (SS-2) support to Qemu. I'm using Linux source as the reference, but some things are not very clear.

Specifically, how are the interrupts routed, which interrupts (ZS, ESP, Lance, timers, floppy, cgthree) go through the interrupt controller? How do the controller bits map to each interrupt source and PIL? Any docs for the controller, what is the device chip ID?

I guess the MMU is similar in operation to Sun4u/v I/D TLB, is that correct? Any CPU docs would be helpful.

There is no IOMMU, so the DMA is limited to lowest 16 megs, right?

#1 lore raw

From: "Blue Swirl" <[email protected]>
Date: Sun, 12 Aug 2007 10:42:33 +0300

Quoted 3 lines

Specifically, how are the interrupts routed, which interrupts (ZS, ESP, Lance, timers, floppy, cgthree) go through the interrupt controller?

There is no interrupt controller, the individual devices and SBUS slot interrupt sources are wired up directly to the cpu.

Quoted 2 lines

How do the controller bits map to each interrupt source and PIL?

All, except 1 or 2, sun4c interrupt sources are non-maskable. There really isn't an interrupt controller at all.

Quoted 1 line

Any docs for the controller, what is the device chip ID?

There are no public docs for this hardware.

Quoted 2 lines

I guess the MMU is similar in operation to Sun4u/v I/D TLB, is that correct? Any CPU docs would be helpful.

Not similar to sun4u at all, and and no public docs.

Quoted 1 line

There is no IOMMU, so the DMA is limited to lowest 16 megs, right?

All DMA is translated by the cpu's MMU as if it were a memory reference made by the cpu itself. Any missed DMA translation watchdog resets the cpu.

author
#2 lore raw
In reply to Blue Swirl
Quoted 6 lines · David Miller
Quoted 3 lines

Specifically, how are the interrupts routed, which interrupts (ZS, ESP, Lance, timers, floppy, cgthree) go through the interrupt controller?

There is no interrupt controller, the individual devices and SBUS slot interrupt sources are wired up directly to the cpu.

Then what device is poked by arch/sparc/kernel/sun4c_irq.c, I thought this was the controller?

Thanks for the other information. Implementing the MMU is going to be difficult.

#3 lore raw
In reply to Blue Swirl

From: "Blue Swirl" <[email protected]>
Date: Sun, 12 Aug 2007 11:21:30 +0300

Quoted 10 lines · David Miller
Quoted 6 lines
Quoted 3 lines

Specifically, how are the interrupts routed, which interrupts (ZS, ESP, Lance, timers, floppy, cgthree) go through the interrupt controller?

There is no interrupt controller, the individual devices and SBUS slot interrupt sources are wired up directly to the cpu.

Then what device is poked by arch/sparc/kernel/sun4c_irq.c, I thought this was the controller?

It's similar to an interrupt controller, and it can mask certain interrupts.

#4 lore raw
In reply to Blue Swirl

Hi All,

I have been working on the SunMMU used by sun4c (SS1/SS2).

The attached file is work in progress so it may not be 100% correct. You will need a wide screen (150 columns) to view the file.

I am using a SS1 Clone, a disassembler and verious bits of SunOS, Solaris and Linux source code to validate it (and to work out why sun4c Linux is so restrictive in what alterations can be made to the code without breaking things completely).

The linux code for handling the cache is very similar to that used by SunOS/Solaris (different register choices apers to be the only difference).

The only low level code in this area that I might change in the future is the sun4c_complete_all_stores() function in mm/sun4c.c. Reading the context register and then executing 6 nop's should be sufficient, but untill I have sorted out some more fundemental (and as yet un-explained) stability issues I will will not be in a position to test this out properly.

There is source code for Solaris 2.6 on the web. This is the best source code reference you will get for sun4c as the low level code has not been changed significantly from that of the architecture reference kernel (SunOS 4.1 - see Sparc SCD 1.1).

The un-explained Linux features I am currently working on are:

1) 3MB Max size. SunOS remaps first 4MB. If Linux is changed to also remap the first 4MB instead of just 3M it breaks.

2) Linux runs at VMA F0000000. SunOS runs at VMA F8000000. If Linux is changed ro run at VMA F80000000 it breaks.

David/Anton may be able to explain these to me quickly :) but working through the code slowly will probably teach me more about what is going on.

If Anton and/or David have the time to brouse through my simple SunMMU file and comment in it it might be helpfull.

Regards
Mark Fortescue.

Quoted 8 lines · Blue Swirl
Quoted 6 lines · David Miller
Quoted 3 lines

Specifically, how are the interrupts routed, which interrupts (ZS, ESP, Lance, timers, floppy, cgthree) go through the interrupt controller?

There is no interrupt controller, the individual devices and SBUS slot interrupt sources are wired up directly to the cpu.

Quoted 9 lines

Then what device is poked by arch/sparc/kernel/sun4c_irq.c, I thought this was the controller?

Thanks for the other information. Implementing the MMU is going to be difficult. -
To unsubscribe from this list: send the line "unsubscribe sparclinux" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

SunMMU:

There are two versions, a 2-Level Version and a 3 Level Version.

The 2-Level version is what is found on the sun4c.

Looking at Linux sparc32 head.S, the 3-Level implementation is found on Sun4/4xx and is described as Sun4-Mutant-MMU.

----------------------------------------------------------------------------------------------------------------------------------------

2-Level Sun4c Implementation:

Virtual Address: 1GB organised as 512MB (Low) + 512MB (High) Physical Address: 256MB OBMEM + 256MB OBIO

32bit Virtual Address is the 30bit Virtual Address Sign Extended to 32bits. The result is that there is a 3GB Hole in the middle of the 32bit Virtual Address space.

                           |--------------->------------------|
                           |                                  |
Virtual Address            |        Physical Address          |
________________________________    _______________________________
| SegmentNum | PTENum | Offset |    | I/O | PageFrameNum | Offset |
|------------|--------|--------|    |-----|--------------|--------|
 29        18 17    12 11     0      31 28 27          12 11     0
      |           |                    |         |
      |           |                    |         |------------------------------------------<-----------------------------------------|
      |           |                    |                                                                                              |
      |           |                    |-------------------------------<--------------------------------|                             |
      |           |                                                                                     |                             |
      |           |------------------->---------------------|                                           |                             |
      |                                                     |                                           |(I/O Sign Extended to 4bits) |
      |---------->------------|                             |                                           |                             |
                              |                             |                                           |                             |
              SMEIndex        |            SME*             |                                           |                             |
              ________________________     _____________    |                                           |                             |
        |-->--| Context | SegmentNum | ->  | GROUP-NUM |    |                                           |                             |
        |     |---------|------------|     |-----------|    |                                           |                             |
        |      c+11   12 11         0       7         0     |                                           |                             |
        |                                        |          |                                           |                             |
        |                                        |          |                                           |                             |
Context |                         PTEIndex       |          |         PTE                               |                             |
___________                       _______________________________     _______________________________________________________________________
| Context | ---------->---------- | Context | GroupNum | PTENum | ->  | VALID | PROT  | NO-CACHE |  TYPE   | REF | MOD | resvd | PAGE-FRAME |
|---------|                       |---------|----------|--------|     |       | W | S |          | 0 | I/O |     |     |       |            |
 c   |   0                         c+g+5 g+6 g+5      6 5      0      |-------|---|---|----------|---|-----|-----|-----|-------|------------|
     |                                                                    31    30  29     28      27   26    25    24  23   16 15         0

c=log2(NoContexts) g=log2(NoGroups)
VALID Page Valid Bit
PROT Access Protection
W Write Enable Bit
S System Page Bit
NO-CACHE No Cache Bit
TYPE Page Type
00 On Board Memory
01 On Board I/O
1x Undefined
REF Referenced Bit (Hardware)
MOD Modified Bit (Hardware)
resvd Reserved (Reads as Zero on SS1)
PAGE-FRAME Page Frame Number

PTE's are accessed at ASI_CTL[CONTEXT_REG] + ASI_PM[vaddr]
SME's are accessed at ASI_CTL[CONTEXT_REG] + ASI_SM[vaddr] *sun4c SME may be > 8bits but PROM V0 only uses GroupNum 0 to 127.
NoGroups : Read from Prom or use 128 (<= 4096)
NoContexts : Read from Prom or use 8

#5 lore raw
In reply to Blue Swirl

Hi David,

Quoted 4 lines

All DMA is translated by the cpu's MMU as if it were a memory reference made by the cpu itself. Any missed DMA translation watchdog resets the cpu.

I am getting a watchdog reset as soon as I enable interrupts after rebooting the kernel from SunOS or a prom_halt. Could in-proper DMA settings be the cause of this?
After the watchdog reset or a powre-up reset, there is no problem booting Linux !.

Note: I am using SunOS4.1.1 boot so the kernel mappings for linux are not what the loader is expecting.

SunOS does not have this problem but they hard code things so that they can get interrupts etc. up and running very much earlier on than Linux so it is very dificult to compare the sequence of events during boot for the two kernels.

#6 lore raw
In reply to Blue Swirl
Quoted 8 lines · David Miller

From: "Blue Swirl" <[email protected]>
Date: Sun, 12 Aug 2007 11:21:30 +0300

Quoted 5 lines · David Miller
Quoted 4 lines
Quoted 3 lines

Specifically, how are the interrupts routed, which interrupts (ZS, ESP, Lance, timers, floppy, cgthree) go through the interrupt controller?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

There is no interrupt controller, the individual devices and SBUS slot interrupt sources are wired up directly to the cpu.

Then what device is poked by arch/sparc/kernel/sun4c_irq.c, I thought this was the controller?

It's similar to an interrupt controller, and it can mask certain interrupts.

Two key features that the Solaris kernel has specific coding for are:

1) Level 15 interrupts can't be masked so Solaris has a counter to keep track of how many times its level 15 interrupt has been interrupted :).

2) On some (if not all) sun4c hardware, interrupts can get through when the interrupt mask is being changed to a higher level. Solaris checks the interrupt level and if it is higher that that required for the interrupt, it just returns from the interrupt as the processor will get interrupted again, properly, as soon as the interrupt mask is lowered to a sufficient level to alow the interrupt to be processed properly.

The Linux interrupt handler probably does the same sort of things but as yet, I have not taken a sufficiently close look at it to confirm this. It may need tweeking.

Regards
Mark Fortescue.

author
#7 lore raw
In reply to Blue Swirl
Quoted 2 lines · Mark Fortescue

The attached file is work in progress so it may not be 100% correct. You will need a wide screen (150 columns) to view the file.

Thanks! So basically the virtual to physical translation can be emulated by the following code:

#include <inttypes.h>
typedef uint32_t target_phys_addr_t;
typedef uint32_t target_ulong;

#define PAGE_READ      0x0001
#define PAGE_WRITE     0x0002
#define PAGE_EXEC      0x0004

#define GROUP_BITS 8
#define NGROUPS (1 << GROUP_BITS)
#define NCONTEXTS 16

#define PG4C_VALID_BIT    31
#define PG4C_W_BIT        30
#define PG4C_S_BIT        29
#define PG4C_CACHE_BIT    28
#define PG4C_IO_BIT       26

#define PG4C_ACCESSED_BIT 25
#define PG4C_MODIFIED_BIT 24

#define PG4C_VALID_MASK    (1 << PG4C_VALID_BIT)
#define PG4C_W_MASK        (1 << PG4C_W_BIT)
#define PG4C_S_MASK        (1 << PG4C_S_BIT)
#define PG4C_CACHE_MASK    (1 << PG4C_CACHE_BIT)
#define PG4C_IO_MASK       (1 << PG4C_IO_BIT)

#define PG4C_ACCESSED_MASK (1 << PG4C_ACCESSED_BIT) #define PG4C_MODIFIED_MASK (1 << PG4C_MODIFIED_BIT)

typedef struct CPUState {
    uint8_t smes[NCONTEXTS * 2048];
    uint32_t ptes[NCONTEXTS * NGROUPS * 64];
    uint8_t sun4c_context;
    uint32_t sun4c_afsr, sun4c_afar;
} CPUState;

int sun4c_get_physical_address (CPUState *env, target_phys_addr_t *physical,
int *prot, int *access_index, target_ulong address,
int rw, int is_user)

{
    uint32_t segment_num, pte_num, offset, pte, pte_index, group_index;
    uint8_t group;
    int is_dirty;

    segment_num = (address & 0x3ffc0000) >> 17;
    pte_num = (address & 0x0003ff800) >> 11;
    offset = address & 0x7ff;
    group_index = (env->sun4c_context << 12) | segment_num;
    group = env->smes[group_index];
    pte_index = (env->sun4c_context << (GROUP_BITS + 6)) | (group << 6) |
        pte_num;
    pte = env->ptes[pte_index];
    if (!(pte & PG4C_VALID_MASK))
        return 1;

    /* update page modified and dirty bits */
    is_dirty = (rw & 1) && !(pte & PG4C_MODIFIED_MASK);
    if (!(pte & PG4C_ACCESSED_MASK) || is_dirty) {
	pte |= PG4C_ACCESSED_MASK;
	if (is_dirty)
	    pte |= PG4C_MODIFIED_MASK;
        env->ptes[pte_index] = pte;
    }

    /* check access */
    if (is_user && (pte & PG4C_S_MASK))
        return 2;
    if ((rw & 1) && (pte & PG4C_W_MASK))
        return 4;

    /* the page can be put in the TLB */
    *prot = PAGE_READ | PAGE_EXEC;
    if (pte & PG4C_W_MASK)
        *prot |= PAGE_WRITE;

    if (!(pte & PG4C_MODIFIED_MASK)) {
        /* only set write access if already dirty... otherwise wait
           for dirty access */
        *prot &= ~PAGE_WRITE;
    }

    /* get physical address */
    *physical = ((pte & PG4C_IO_MASK)? 0xf0000000 : 0) | ((pte & 0xffff) << 12)
        | offset;

    return 0;
}
#8 lore raw
In reply to Blue Swirl
Quoted 7 lines · Blue Swirl
Quoted 2 lines · Mark Fortescue

The attached file is work in progress so it may not be 100% correct. You will need a wide screen (150 columns) to view the file.

Thanks! So basically the virtual to physical translation can be emulated by the following code:

Quoted 7 lines

#include <inttypes.h>
typedef uint32_t target_phys_addr_t;
typedef uint32_t target_ulong;

#define PAGE_READ      0x0001
#define PAGE_WRITE     0x0002
#define PAGE_EXEC      0x0004

Group bits is variable. For SS1 4/60 it is 7 (hard coded as it is not in V0 Prom), for SS2 4/75 it is 8 (from the V2 prom).

There is code in my SS1 prom that sugests that you should alow for upto 12 bits. You can store an 8bit value in the SME on my SS1 Clone but I have not done any tests to see it it actually has 256 groups, as SunOS4.1.1, Solaris 2.6 and Linux hardcode SS1 to have 128 groups and the SS1 prom does not use groups > 127.

In addition, the Sun4 Linux code assumes > 8 bits.

Quoted 2 lines

#define GROUP_BITS 8
#define NGROUPS (1 << GROUP_BITS)

The number of contexts is variable. For SS2 it is 16 (from V2 prom). For SS1 4/60 it is 8 (hard coded as per no groups).

Quoted 6 lines

#define NCONTEXTS 16

#define PG4C_VALID_BIT    31
#define PG4C_W_BIT        30
#define PG4C_S_BIT        29
#define PG4C_CACHE_BIT    28
#define PG4C_IO_BIT       26
Quoted 8 lines

#define PG4C_ACCESSED_BIT 25
#define PG4C_MODIFIED_BIT 24

#define PG4C_VALID_MASK    (1 << PG4C_VALID_BIT)
#define PG4C_W_MASK        (1 << PG4C_W_BIT)
#define PG4C_S_MASK        (1 << PG4C_S_BIT)
#define PG4C_CACHE_MASK    (1 << PG4C_CACHE_BIT)
#define PG4C_IO_MASK       (1 << PG4C_IO_BIT)

#define PG4C_TYPE_MASK (3 << PG4C_IO_BIT)

#define PG4C_PFN_SIZE_BITS 16
#define PG4C_PFN_MASK ((1 << PG4C_PFN_SIZE_BITS) - 1)

#define PG4C_PFN_SHIFT 12
#define PG4C_PAGE_MASK ((1 << PG4C_PFN_SHIFT) - 1)

The value for OBMEM is 0, for OBIO is 1. Other values crash my SS1 but going through solaris code, it looks like there is another state that is in theory valid.

Quoted 5 lines

#define PG4C_ACCESSED_MASK (1 << PG4C_ACCESSED_BIT) #define PG4C_MODIFIED_MASK (1 << PG4C_MODIFIED_BIT)

typedef struct CPUState {
   uint8_t smes[NCONTEXTS * 2048];

uint8_t smes[NCONTEXTS * 4096];

Quoted 9 lines
   uint32_t ptes[NCONTEXTS * NGROUPS * 64];
   uint8_t sun4c_context;
   uint32_t sun4c_afsr, sun4c_afar;
} CPUState;

int sun4c_get_physical_address (CPUState *env, target_phys_addr_t *physical,
int *prot, int *access_index, target_ulong address,
int rw, int is_user)

Quoted 6 lines
{
   uint32_t segment_num, pte_num, offset, pte, pte_index, group_index;
   uint8_t group;
   int is_dirty;

   segment_num = (address & 0x3ffc0000) >> 17;

segment_num = (address & 0x3ffc0000) >> (PG4C_PFN_SHIFT + 6); /* 18 */

Quoted 1 line
   pte_num = (address & 0x0003ff800) >> 11;

pte_num = (address & 0x0003ff000) >> PG4C_PFN_SHIFT; /* 12 */

Quoted 1 line
   offset = address & 0x7ff;

offset = address & PG4C_PAGE_MASK; /* 0xfff */

Quoted 8 lines
   group_index = (env->sun4c_context << 12) | segment_num;
   group = env->smes[group_index];
   pte_index = (env->sun4c_context << (GROUP_BITS + 6)) | (group << 6) |
       pte_num;
   pte = env->ptes[pte_index];
   if (!(pte & PG4C_VALID_MASK))
       return 1;
Quoted 9 lines
   /* update page modified and dirty bits */
   is_dirty = (rw & 1) && !(pte & PG4C_MODIFIED_MASK);
   if (!(pte & PG4C_ACCESSED_MASK) || is_dirty) {
	pte |= PG4C_ACCESSED_MASK;
	if (is_dirty)
	    pte |= PG4C_MODIFIED_MASK;
       env->ptes[pte_index] = pte;
   }

I am not sure in when the PTE accessed bit (PG4C_ACCESSED_MASK) gets set. It definatly gets accesed on write but not always on read. It may only be set on read if the cache is empty (or on DMA)?

Quoted 17 lines
   /* check access */
   if (is_user && (pte & PG4C_S_MASK))
       return 2;
   if ((rw & 1) && (pte & PG4C_W_MASK))
       return 4;

   /* the page can be put in the TLB */
   *prot = PAGE_READ | PAGE_EXEC;
   if (pte & PG4C_W_MASK)
       *prot |= PAGE_WRITE;

   if (!(pte & PG4C_MODIFIED_MASK)) {
       /* only set write access if already dirty... otherwise wait
          for dirty access */
       *prot &= ~PAGE_WRITE;
   }
Quoted 3 lines
   /* get physical address */
   *physical = ((pte & PG4C_IO_MASK)? 0xf0000000 : 0) | ((pte & 0xffff) << 12)
       | offset;

*physical = ((pte & PG4C_TYPE_MASK) = PG4C_IO_MASK ? 0xf0000000 : 0) | ((pte & PG4C_PFN_MASK) << PG4C_PFN_SHIFT) | offset

Quoted 3 lines
   return 0;
}

PET bits 0 to 15 and 24 to 31 are software modifiable on my sun4c. The eight bits in the middle (16 .. 23) always read as 0. SunOS 4.1.1 always writes to these middle bits (maybe because the Page Frame Number on Sun4 is 19 or 20 bits, not 16bits or maybe the engineers were catering from some sun4c hardware not known to us :) ).

As a short note: A Sun4 is very similar to a Sun4c so coding the sun4c stuf up using more #defines may have be benificial. From what I have seen in the Linux code, the register/prom addresses are the same as on my SS1. One big execption is that some of them use a three level SunMMU. The other would apear to be the page size (offset of 13 bits, not 12 bits). Without the assistance of someone with various different versions of Sun4 that can be used to try out things, I can't verify if Linux actually maps directly to the physical hardware or works for some other reason. Best to assume it does untill proven otherwise.

I will try to update my diagram to include sun4 2 and 3 level SunMMU's based on the Linux coding.

Regards
Mark Fortescue.

#9 lore raw
In reply to Blue Swirl

From: Mark Fortescue <[email protected]>
Date: Sun, 12 Aug 2007 15:33:45 +0100 (BST)

Quoted 11 lines

Hi David,

Quoted 3 lines

All DMA is translated by the cpu's MMU as if it were a memory reference made by the cpu itself. Any missed DMA translation watchdog resets the cpu.

I am getting a watchdog reset as soon as I enable interrupts after rebooting the kernel from SunOS or a prom_halt. Could in-proper DMA settings be the cause of this?
After the watchdog reset or a powre-up reset, there is no problem booting Linux !.

It's likely taking interrupts like crazy because of the masking issue. Since no devices have registered interrupt handlers yet, the interrupt source is never ACK'd and thus the interrupt never shuts up.

Most of the time this happens with the sunlance chip, perhaps this is what is being left enabled by SunOS.

Build a debugging kernel with some printk()'s added to arch/sparc/kernel/irq.c:handler_irq(), just print the IRQ number and the trap PC or similar. Test boot it in the case that crashes and record the messages, you only need the first one to figure out which IRQ is active when interrupts are enabled.

It is my recollection that at least one of the BSDs had detection for this issue and it would print something like "crazy interrupts!" in the kernel log when the condition was detected. Indeed, it's a good grepping point :-)

davem@sunset:~/src/OpenBSD/cvs/src/sys/arch/sparc/sparc$ egrep -i "crazy interrupts" *
intr.c:			panic("crazy interrupts");
davem@sunset:~/src/OpenBSD/cvs/src/sys/arch/sparc/sparc$ 

There are some things we can do to deal with this.

Let's assume you find out for sure it's sunlance, then in that case you can write a small mini-of_device driver whose only purpose is to shut off the interrupts in all the sunlance devices in the machine. You execute it right after the of_device layer initializes.

static int __init lance_irq_disable_probe(struct of_device *dev,
const struct of_device_id *match)

{
	void __iomem *regs;

	regs = of_ioremap(&dev->resource[0], 0, 0x04, "lance_irq");
	if (regs) {
		sbus_writew(0x0000, regs + 0x02UL); /* ADDRESS = LE_CSR0 */
		sbus_writew(0x0004, regs + 0x00UL); /* LE_C0_STOP */
		of_iounmap(&dev->resource[0], regs, 0x04);
	}
	return 0;
}
static struct of_device_id lance_irq_disable_match[] = {
	{
		.name = "le",
	},
	{},
};

static struct of_platform_driver lance_irq_disable_driver = {
.name = "sunlance",
.match_table = lance_irq_disable_match,
.probe = lance_irq_disable_probe,
};

	...

	if (!of_register_driver(&lance_irq_disable_driver,
				&of_platform_bus_type))
		of_unregister_driver(&lance_irq_disable_driver,
				     &of_platform_bus_type);

You can repeat this process for any other device you determine that SunOS leaves IRQs enabled for.