Merge branch 'vendor/LIBARCHIVE'
[dragonfly.git] / sys / platform / pc64 / icu / icu_abi.c
1 /*
2  * Copyright (c) 1991 The Regents of the University of California.
3  * Copyright (c) 2005,2008 The DragonFly Project.
4  * All rights reserved.
5  * 
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  *
9  * This code is derived from software contributed to Berkeley by
10  * William Jolitz.
11  * 
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in
20  *    the documentation and/or other materials provided with the
21  *    distribution.
22  * 3. Neither the name of The DragonFly Project nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific, prior written permission.
25  * 
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
30  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
32  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  * 
39  * $DragonFly: src/sys/platform/pc64/icu/icu_abi.c,v 1.1 2008/08/29 17:07:16 dillon Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/machintr.h>
46 #include <sys/interrupt.h>
47 #include <sys/rman.h>
48 #include <sys/bus.h>
49
50 #include <machine/segments.h>
51 #include <machine/md_var.h>
52 #include <machine/intr_machdep.h>
53 #include <machine/globaldata.h>
54 #include <machine/smp.h>
55 #include <machine/msi_var.h>
56
57 #include <sys/thread2.h>
58
59 #include <machine_base/icu/elcr_var.h>
60
61 #include <machine_base/icu/icu.h>
62 #include <machine_base/icu/icu_ipl.h>
63 #include <machine_base/apic/ioapic.h>
64
65 extern inthand_t
66         IDTVEC(icu_intr0),      IDTVEC(icu_intr1),
67         IDTVEC(icu_intr2),      IDTVEC(icu_intr3),
68         IDTVEC(icu_intr4),      IDTVEC(icu_intr5),
69         IDTVEC(icu_intr6),      IDTVEC(icu_intr7),
70         IDTVEC(icu_intr8),      IDTVEC(icu_intr9),
71         IDTVEC(icu_intr10),     IDTVEC(icu_intr11),
72         IDTVEC(icu_intr12),     IDTVEC(icu_intr13),
73         IDTVEC(icu_intr14),     IDTVEC(icu_intr15);
74
75 static inthand_t *icu_intr[ICU_HWI_VECTORS] = {
76         &IDTVEC(icu_intr0),     &IDTVEC(icu_intr1),
77         &IDTVEC(icu_intr2),     &IDTVEC(icu_intr3),
78         &IDTVEC(icu_intr4),     &IDTVEC(icu_intr5),
79         &IDTVEC(icu_intr6),     &IDTVEC(icu_intr7),
80         &IDTVEC(icu_intr8),     &IDTVEC(icu_intr9),
81         &IDTVEC(icu_intr10),    &IDTVEC(icu_intr11),
82         &IDTVEC(icu_intr12),    &IDTVEC(icu_intr13),
83         &IDTVEC(icu_intr14),    &IDTVEC(icu_intr15)
84 };
85
86 static struct icu_irqmap {
87         int                     im_type;        /* ICU_IMT_ */
88         enum intr_trigger       im_trig;
89         int                     im_msi_base;
90 } icu_irqmaps[MAXCPU][IDT_HWI_VECTORS];
91
92 static struct lwkt_token icu_irqmap_tok =
93         LWKT_TOKEN_INITIALIZER(icu_irqmap_token);
94
95 #define ICU_IMT_UNUSED          0       /* KEEP THIS */
96 #define ICU_IMT_RESERVED        1
97 #define ICU_IMT_LEGACY          2
98 #define ICU_IMT_SYSCALL         3
99 #define ICU_IMT_MSI             4
100
101 #define ICU_IMT_ISHWI(map)      ((map)->im_type != ICU_IMT_RESERVED && \
102                                  (map)->im_type != ICU_IMT_SYSCALL)
103
104 extern void     ICU_INTREN(int);
105 extern void     ICU_INTRDIS(int);
106
107 extern int      imcr_present;
108
109 static void     icu_abi_intr_enable(int);
110 static void     icu_abi_intr_disable(int);
111 static void     icu_abi_intr_setup(int, int);
112 static void     icu_abi_intr_teardown(int);
113
114 static void     icu_abi_legacy_intr_config(int, enum intr_trigger,
115                     enum intr_polarity);
116 static int      icu_abi_legacy_intr_cpuid(int);
117
118 static int      icu_abi_msi_alloc(int [], int, int);
119 static void     icu_abi_msi_release(const int [], int, int);
120 static void     icu_abi_msi_map(int, uint64_t *, uint32_t *, int);
121
122 static void     icu_abi_finalize(void);
123 static void     icu_abi_cleanup(void);
124 static void     icu_abi_setdefault(void);
125 static void     icu_abi_stabilize(void);
126 static void     icu_abi_initmap(void);
127 static void     icu_abi_rman_setup(struct rman *);
128
129 struct machintr_abi MachIntrABI_ICU = {
130         MACHINTR_ICU,
131         .intr_disable   = icu_abi_intr_disable,
132         .intr_enable    = icu_abi_intr_enable,
133         .intr_setup     = icu_abi_intr_setup,
134         .intr_teardown  = icu_abi_intr_teardown,
135
136         .legacy_intr_config = icu_abi_legacy_intr_config,
137         .legacy_intr_cpuid = icu_abi_legacy_intr_cpuid,
138
139         .msi_alloc      = icu_abi_msi_alloc,
140         .msi_release    = icu_abi_msi_release,
141         .msi_map        = icu_abi_msi_map,
142
143         .finalize       = icu_abi_finalize,
144         .cleanup        = icu_abi_cleanup,
145         .setdefault     = icu_abi_setdefault,
146         .stabilize      = icu_abi_stabilize,
147         .initmap        = icu_abi_initmap,
148         .rman_setup     = icu_abi_rman_setup
149 };
150
151 static int      icu_abi_msi_start;      /* NOTE: for testing only */
152
153 /*
154  * WARNING!  SMP builds can use the ICU now so this code must be MP safe.
155  */
156
157 static void
158 icu_abi_intr_enable(int irq)
159 {
160         const struct icu_irqmap *map;
161
162         KASSERT(irq >= 0 && irq < IDT_HWI_VECTORS,
163             ("icu enable, invalid irq %d\n", irq));
164
165         map = &icu_irqmaps[mycpuid][irq];
166         KASSERT(ICU_IMT_ISHWI(map),
167             ("icu enable, not hwi irq %d, type %d, cpu%d\n",
168              irq, map->im_type, mycpuid));
169         if (map->im_type != ICU_IMT_LEGACY)
170                 return;
171
172         ICU_INTREN(irq);
173 }
174
175 static void
176 icu_abi_intr_disable(int irq)
177 {
178         const struct icu_irqmap *map;
179
180         KASSERT(irq >= 0 && irq < IDT_HWI_VECTORS,
181             ("icu disable, invalid irq %d\n", irq));
182
183         map = &icu_irqmaps[mycpuid][irq];
184         KASSERT(ICU_IMT_ISHWI(map),
185             ("icu disable, not hwi irq %d, type %d, cpu%d\n",
186              irq, map->im_type, mycpuid));
187         if (map->im_type != ICU_IMT_LEGACY)
188                 return;
189
190         ICU_INTRDIS(irq);
191 }
192
193 /*
194  * Called before interrupts are physically enabled
195  */
196 static void
197 icu_abi_stabilize(void)
198 {
199         int intr;
200
201         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr)
202                 ICU_INTRDIS(intr);
203         ICU_INTREN(ICU_IRQ_SLAVE);
204 }
205
206 /*
207  * Called after interrupts physically enabled but before the
208  * critical section is released.
209  */
210 static void
211 icu_abi_cleanup(void)
212 {
213         bzero(mdcpu->gd_ipending, sizeof(mdcpu->gd_ipending));
214 }
215
216 /*
217  * Called after stablize and cleanup; critical section is not
218  * held and interrupts are not physically disabled.
219  */
220 static void
221 icu_abi_finalize(void)
222 {
223         KKASSERT(MachIntrABI.type == MACHINTR_ICU);
224         KKASSERT(!ioapic_enable);
225
226         /*
227          * If an IMCR is present, programming bit 0 disconnects the 8259
228          * from the BSP.  The 8259 may still be connected to LINT0 on the
229          * BSP's LAPIC.
230          *
231          * If we are running SMP the LAPIC is active, try to use virtual
232          * wire mode so we can use other interrupt sources within the LAPIC
233          * in addition to the 8259.
234          */
235         if (imcr_present) {
236                 outb(0x22, 0x70);
237                 outb(0x23, 0x01);
238         }
239 }
240
241 static void
242 icu_abi_intr_setup(int intr, int flags)
243 {
244         const struct icu_irqmap *map;
245         register_t ef;
246
247         KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
248             ("icu setup, invalid irq %d\n", intr));
249
250         map = &icu_irqmaps[mycpuid][intr];
251         KASSERT(ICU_IMT_ISHWI(map),
252             ("icu setup, not hwi irq %d, type %d, cpu%d\n",
253              intr, map->im_type, mycpuid));
254         if (map->im_type != ICU_IMT_LEGACY)
255                 return;
256
257         ef = read_rflags();
258         cpu_disable_intr();
259
260         ICU_INTREN(intr);
261
262         write_rflags(ef);
263 }
264
265 static void
266 icu_abi_intr_teardown(int intr)
267 {
268         const struct icu_irqmap *map;
269         register_t ef;
270
271         KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
272             ("icu teardown, invalid irq %d\n", intr));
273
274         map = &icu_irqmaps[mycpuid][intr];
275         KASSERT(ICU_IMT_ISHWI(map),
276             ("icu teardown, not hwi irq %d, type %d, cpu%d\n",
277              intr, map->im_type, mycpuid));
278         if (map->im_type != ICU_IMT_LEGACY)
279                 return;
280
281         ef = read_rflags();
282         cpu_disable_intr();
283
284         ICU_INTRDIS(intr);
285
286         write_rflags(ef);
287 }
288
289 static void
290 icu_abi_setdefault(void)
291 {
292         int intr;
293
294         for (intr = 0; intr < ICU_HWI_VECTORS; ++intr) {
295                 if (intr == ICU_IRQ_SLAVE)
296                         continue;
297                 setidt_global(IDT_OFFSET + intr, icu_intr[intr],
298                     SDT_SYSIGT, SEL_KPL, 0);
299         }
300 }
301
302 static void
303 icu_abi_initmap(void)
304 {
305         int cpu;
306
307         kgetenv_int("hw.icu.msi_start", &icu_abi_msi_start);
308         icu_abi_msi_start &= ~0x1f;     /* MUST be 32 aligned */
309
310         /*
311          * NOTE: ncpus is not ready yet
312          */
313         for (cpu = 0; cpu < MAXCPU; ++cpu) {
314                 int i;
315
316                 if (cpu != 0) {
317                         for (i = 0; i < ICU_HWI_VECTORS; ++i)
318                                 icu_irqmaps[cpu][i].im_type = ICU_IMT_RESERVED;
319                 } else {
320                         for (i = 0; i < ICU_HWI_VECTORS; ++i)
321                                 icu_irqmaps[cpu][i].im_type = ICU_IMT_LEGACY;
322                         icu_irqmaps[cpu][ICU_IRQ_SLAVE].im_type =
323                             ICU_IMT_RESERVED;
324
325                         if (elcr_found) {
326                                 for (i = 0; i < ICU_HWI_VECTORS; ++i) {
327                                         icu_irqmaps[cpu][i].im_trig =
328                                             elcr_read_trigger(i);
329                                 }
330                         } else {
331                                 /*
332                                  * NOTE: Trigger mode does not matter at all
333                                  */
334                                 for (i = 0; i < ICU_HWI_VECTORS; ++i) {
335                                         icu_irqmaps[cpu][i].im_trig =
336                                             INTR_TRIGGER_EDGE;
337                                 }
338                         }
339                 }
340
341                 for (i = 0; i < IDT_HWI_VECTORS; ++i)
342                         icu_irqmaps[cpu][i].im_msi_base = -1;
343
344                 icu_irqmaps[cpu][IDT_OFFSET_SYSCALL - IDT_OFFSET].im_type =
345                     ICU_IMT_SYSCALL;
346         }
347 }
348
349 static void
350 icu_abi_legacy_intr_config(int irq, enum intr_trigger trig,
351     enum intr_polarity pola __unused)
352 {
353         struct icu_irqmap *map;
354
355         KKASSERT(trig == INTR_TRIGGER_EDGE || trig == INTR_TRIGGER_LEVEL);
356
357         KKASSERT(irq >= 0 && irq < IDT_HWI_VECTORS);
358         map = &icu_irqmaps[0][irq];
359
360         KKASSERT(map->im_type == ICU_IMT_LEGACY);
361
362         /* TODO: Check whether it is configured or not */
363
364         if (trig == map->im_trig)
365                 return;
366
367         if (bootverbose) {
368                 kprintf("ICU: irq %d, %s -> %s\n", irq,
369                         intr_str_trigger(map->im_trig),
370                         intr_str_trigger(trig));
371         }
372         map->im_trig = trig;
373
374         if (!elcr_found) {
375                 if (bootverbose)
376                         kprintf("ICU: no ELCR, skip irq %d config\n", irq);
377                 return;
378         }
379         elcr_write_trigger(irq, map->im_trig);
380 }
381
382 static int
383 icu_abi_legacy_intr_cpuid(int irq __unused)
384 {
385         return 0;
386 }
387
388 static void
389 icu_abi_rman_setup(struct rman *rm)
390 {
391         int start, end, i;
392
393         KASSERT(rm->rm_cpuid >= 0 && rm->rm_cpuid < MAXCPU,
394             ("invalid rman cpuid %d", rm->rm_cpuid));
395
396         start = end = -1;
397         for (i = 0; i < IDT_HWI_VECTORS; ++i) {
398                 const struct icu_irqmap *map = &icu_irqmaps[rm->rm_cpuid][i];
399
400                 if (start < 0) {
401                         if (ICU_IMT_ISHWI(map))
402                                 start = end = i;
403                 } else {
404                         if (ICU_IMT_ISHWI(map)) {
405                                 end = i;
406                         } else {
407                                 KKASSERT(end >= 0);
408                                 if (bootverbose) {
409                                         kprintf("ICU: rman cpu%d %d - %d\n",
410                                             rm->rm_cpuid, start, end);
411                                 }
412                                 if (rman_manage_region(rm, start, end)) {
413                                         panic("rman_manage_region"
414                                             "(cpu%d %d - %d)", rm->rm_cpuid,
415                                             start, end);
416                                 }
417                                 start = end = -1;
418                         }
419                 }
420         }
421         if (start >= 0) {
422                 KKASSERT(end >= 0);
423                 if (bootverbose) {
424                         kprintf("ICU: rman cpu%d %d - %d\n",
425                             rm->rm_cpuid, start, end);
426                 }
427                 if (rman_manage_region(rm, start, end)) {
428                         panic("rman_manage_region(cpu%d %d - %d)",
429                             rm->rm_cpuid, start, end);
430                 }
431         }
432 }
433
434 static int
435 icu_abi_msi_alloc(int intrs[], int count, int cpuid)
436 {
437         int i, error;
438
439         KASSERT(cpuid >= 0 && cpuid < ncpus,
440             ("invalid cpuid %d", cpuid));
441
442         KASSERT(count > 0 && count <= 32, ("invalid count %d\n", count));
443         KASSERT((count & (count - 1)) == 0,
444             ("count %d is not power of 2\n", count));
445
446         lwkt_gettoken(&icu_irqmap_tok);
447
448         /*
449          * NOTE:
450          * Since IDT_OFFSET is 32, which is the maximum valid 'count',
451          * we do not need to find out the first properly aligned
452          * interrupt vector.
453          */
454
455         error = EMSGSIZE;
456         for (i = icu_abi_msi_start; i < IDT_HWI_VECTORS; i += count) {
457                 int j;
458
459                 if (icu_irqmaps[cpuid][i].im_type != ICU_IMT_UNUSED)
460                         continue;
461
462                 for (j = 1; j < count; ++j) {
463                         if (icu_irqmaps[cpuid][i + j].im_type != ICU_IMT_UNUSED)
464                                 break;
465                 }
466                 if (j != count)
467                         continue;
468
469                 for (j = 0; j < count; ++j) {
470                         struct icu_irqmap *map;
471                         int intr = i + j;
472
473                         map = &icu_irqmaps[cpuid][intr];
474                         KASSERT(map->im_msi_base < 0,
475                             ("intr %d, stale MSI-base %d\n",
476                              intr, map->im_msi_base));
477
478                         map->im_type = ICU_IMT_MSI;
479                         map->im_msi_base = i;
480
481                         intrs[j] = intr;
482                         msi_setup(intr, cpuid);
483
484                         if (bootverbose) {
485                                 kprintf("alloc MSI intr %d on cpu%d\n",
486                                     intr, cpuid);
487                         }
488                 }
489                 error = 0;
490                 break;
491         }
492
493         lwkt_reltoken(&icu_irqmap_tok);
494
495         return error;
496 }
497
498 static void
499 icu_abi_msi_release(const int intrs[], int count, int cpuid)
500 {
501         int i, msi_base = -1, intr_next = -1, mask;
502
503         KASSERT(cpuid >= 0 && cpuid < ncpus,
504             ("invalid cpuid %d", cpuid));
505
506         KASSERT(count > 0 && count <= 32, ("invalid count %d\n", count));
507
508         mask = count - 1;
509         KASSERT((count & mask) == 0, ("count %d is not power of 2\n", count));
510
511         lwkt_gettoken(&icu_irqmap_tok);
512
513         for (i = 0; i < count; ++i) {
514                 struct icu_irqmap *map;
515                 int intr = intrs[i];
516
517                 KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
518                     ("invalid intr %d\n", intr));
519
520                 map = &icu_irqmaps[cpuid][intr];
521                 KASSERT(map->im_type == ICU_IMT_MSI,
522                     ("try release non-MSI intr %d, type %d\n",
523                      intr, map->im_type));
524                 KASSERT(map->im_msi_base >= 0 && map->im_msi_base <= intr,
525                     ("intr %d, invalid MSI-base %d\n", intr, map->im_msi_base));
526                 KASSERT((map->im_msi_base & mask) == 0,
527                     ("intr %d, MSI-base %d is not proper aligned %d\n",
528                      intr, map->im_msi_base, count));
529
530                 if (msi_base < 0) {
531                         msi_base = map->im_msi_base;
532                 } else {
533                         KASSERT(map->im_msi_base == msi_base,
534                             ("intr %d, inconsistent MSI-base, "
535                              "was %d, now %d\n",
536                              intr, msi_base, map->im_msi_base));
537                 }
538
539                 if (intr_next < intr)
540                         intr_next = intr;
541
542                 map->im_type = ICU_IMT_UNUSED;
543                 map->im_msi_base = -1;
544
545                 if (bootverbose)
546                         kprintf("release MSI intr %d on cpu%d\n", intr, cpuid);
547         }
548
549         KKASSERT(intr_next > 0);
550         KKASSERT(msi_base >= 0);
551
552         ++intr_next;
553         if (intr_next < IDT_HWI_VECTORS) {
554                 const struct icu_irqmap *map = &icu_irqmaps[cpuid][intr_next];
555
556                 if (map->im_type == ICU_IMT_MSI) {
557                         KASSERT(map->im_msi_base != msi_base,
558                             ("more than %d MSI was allocated\n", count));
559                 }
560         }
561
562         lwkt_reltoken(&icu_irqmap_tok);
563 }
564
565 static void
566 icu_abi_msi_map(int intr, uint64_t *addr, uint32_t *data, int cpuid)
567 {
568         const struct icu_irqmap *map;
569
570         KASSERT(cpuid >= 0 && cpuid < ncpus,
571             ("invalid cpuid %d", cpuid));
572
573         KASSERT(intr >= 0 && intr < IDT_HWI_VECTORS,
574             ("invalid intr %d\n", intr));
575
576         lwkt_gettoken(&icu_irqmap_tok);
577
578         map = &icu_irqmaps[cpuid][intr];
579         KASSERT(map->im_type == ICU_IMT_MSI,
580             ("try map non-MSI intr %d, type %d\n", intr, map->im_type));
581         KASSERT(map->im_msi_base >= 0 && map->im_msi_base <= intr,
582             ("intr %d, invalid MSI-base %d\n", intr, map->im_msi_base));
583
584         msi_map(map->im_msi_base, addr, data, cpuid);
585
586         if (bootverbose)
587                 kprintf("map MSI intr %d on cpu%d\n", intr, cpuid);
588
589         lwkt_reltoken(&icu_irqmap_tok);
590 }