tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / gpu / drm / nouveau / core / engine / graph / fuc / hubnvc0.fuc
1 /* fuc microcode for nvc0 PGRAPH/HUB
2  *
3  * Copyright 2011 Red Hat Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: Ben Skeggs
24  */
25
26 /* To build:
27  *    m4 hubnvc0.fuc | envyas -a -w -m fuc -V fuc3 -o hubnvc0.fuc.h
28  */
29
30 .section #nvc0_grhub_data
31 include(`nvc0.fuc')
32 gpc_count:              .b32 0
33 rop_count:              .b32 0
34 cmd_queue:              queue_init
35 hub_mmio_list_head:     .b32 0
36 hub_mmio_list_tail:     .b32 0
37
38 ctx_current:            .b32 0
39
40 chipsets:
41 .b8  0xc0 0 0 0
42 .b16 #nvc0_hub_mmio_head
43 .b16 #nvc0_hub_mmio_tail
44 .b8  0xc1 0 0 0
45 .b16 #nvc0_hub_mmio_head
46 .b16 #nvc1_hub_mmio_tail
47 .b8  0xc3 0 0 0
48 .b16 #nvc0_hub_mmio_head
49 .b16 #nvc0_hub_mmio_tail
50 .b8  0xc4 0 0 0
51 .b16 #nvc0_hub_mmio_head
52 .b16 #nvc0_hub_mmio_tail
53 .b8  0xc8 0 0 0
54 .b16 #nvc0_hub_mmio_head
55 .b16 #nvc0_hub_mmio_tail
56 .b8  0xce 0 0 0
57 .b16 #nvc0_hub_mmio_head
58 .b16 #nvc0_hub_mmio_tail
59 .b8  0xcf 0 0 0
60 .b16 #nvc0_hub_mmio_head
61 .b16 #nvc0_hub_mmio_tail
62 .b8  0xd9 0 0 0
63 .b16 #nvd9_hub_mmio_head
64 .b16 #nvd9_hub_mmio_tail
65 .b8  0xd7 0 0 0
66 .b16 #nvd9_hub_mmio_head
67 .b16 #nvd9_hub_mmio_tail
68 .b8  0 0 0 0
69
70 nvc0_hub_mmio_head:
71 mmctx_data(0x17e91c, 2)
72 mmctx_data(0x400204, 2)
73 mmctx_data(0x404004, 11)
74 mmctx_data(0x404044, 1)
75 mmctx_data(0x404094, 14)
76 mmctx_data(0x4040d0, 7)
77 mmctx_data(0x4040f8, 1)
78 mmctx_data(0x404130, 3)
79 mmctx_data(0x404150, 3)
80 mmctx_data(0x404164, 2)
81 mmctx_data(0x404174, 3)
82 mmctx_data(0x404200, 8)
83 mmctx_data(0x404404, 14)
84 mmctx_data(0x404460, 4)
85 mmctx_data(0x404480, 1)
86 mmctx_data(0x404498, 1)
87 mmctx_data(0x404604, 4)
88 mmctx_data(0x404618, 32)
89 mmctx_data(0x404698, 21)
90 mmctx_data(0x4046f0, 2)
91 mmctx_data(0x404700, 22)
92 mmctx_data(0x405800, 1)
93 mmctx_data(0x405830, 3)
94 mmctx_data(0x405854, 1)
95 mmctx_data(0x405870, 4)
96 mmctx_data(0x405a00, 2)
97 mmctx_data(0x405a18, 1)
98 mmctx_data(0x406020, 1)
99 mmctx_data(0x406028, 4)
100 mmctx_data(0x4064a8, 2)
101 mmctx_data(0x4064b4, 2)
102 mmctx_data(0x407804, 1)
103 mmctx_data(0x40780c, 6)
104 mmctx_data(0x4078bc, 1)
105 mmctx_data(0x408000, 7)
106 mmctx_data(0x408064, 1)
107 mmctx_data(0x408800, 3)
108 mmctx_data(0x408900, 4)
109 mmctx_data(0x408980, 1)
110 nvc0_hub_mmio_tail:
111 mmctx_data(0x4064c0, 2)
112 nvc1_hub_mmio_tail:
113
114 nvd9_hub_mmio_head:
115 mmctx_data(0x17e91c, 2)
116 mmctx_data(0x400204, 2)
117 mmctx_data(0x404004, 10)
118 mmctx_data(0x404044, 1)
119 mmctx_data(0x404094, 14)
120 mmctx_data(0x4040d0, 7)
121 mmctx_data(0x4040f8, 1)
122 mmctx_data(0x404130, 3)
123 mmctx_data(0x404150, 3)
124 mmctx_data(0x404164, 2)
125 mmctx_data(0x404178, 2)
126 mmctx_data(0x404200, 8)
127 mmctx_data(0x404404, 14)
128 mmctx_data(0x404460, 4)
129 mmctx_data(0x404480, 1)
130 mmctx_data(0x404498, 1)
131 mmctx_data(0x404604, 4)
132 mmctx_data(0x404618, 32)
133 mmctx_data(0x404698, 21)
134 mmctx_data(0x4046f0, 2)
135 mmctx_data(0x404700, 22)
136 mmctx_data(0x405800, 1)
137 mmctx_data(0x405830, 3)
138 mmctx_data(0x405854, 1)
139 mmctx_data(0x405870, 4)
140 mmctx_data(0x405a00, 2)
141 mmctx_data(0x405a18, 1)
142 mmctx_data(0x406020, 1)
143 mmctx_data(0x406028, 4)
144 mmctx_data(0x4064a8, 2)
145 mmctx_data(0x4064b4, 5)
146 mmctx_data(0x407804, 1)
147 mmctx_data(0x40780c, 6)
148 mmctx_data(0x4078bc, 1)
149 mmctx_data(0x408000, 7)
150 mmctx_data(0x408064, 1)
151 mmctx_data(0x408800, 3)
152 mmctx_data(0x408900, 4)
153 mmctx_data(0x408980, 1)
154 nvd9_hub_mmio_tail:
155
156 .align 256
157 chan_data:
158 chan_mmio_count:        .b32 0
159 chan_mmio_address:      .b32 0
160
161 .align 256
162 xfer_data:              .b32 0
163
164 .section #nvc0_grhub_code
165 bra #init
166 define(`include_code')
167 include(`nvc0.fuc')
168
169 // reports an exception to the host
170 //
171 // In: $r15 error code (see nvc0.fuc)
172 //
173 error:
174         push $r14
175         mov $r14 0x814
176         shl b32 $r14 6
177         iowr I[$r14 + 0x000] $r15       // CC_SCRATCH[5] = error code
178         mov $r14 0xc1c
179         shl b32 $r14 6
180         mov $r15 1
181         iowr I[$r14 + 0x000] $r15       // INTR_UP_SET
182         pop $r14
183         ret
184
185 // HUB fuc initialisation, executed by triggering ucode start, will
186 // fall through to main loop after completion.
187 //
188 // Input:
189 //   CC_SCRATCH[0]: chipset (PMC_BOOT_0 read returns 0x0bad0bad... sigh)
190 //
191 // Output:
192 //   CC_SCRATCH[0]:
193 //           31:31: set to signal completion
194 //   CC_SCRATCH[1]:
195 //            31:0: total PGRAPH context size
196 //
197 init:
198         clear b32 $r0
199         mov $sp $r0
200         mov $xdbase $r0
201
202         // enable fifo access
203         mov $r1 0x1200
204         mov $r2 2
205         iowr I[$r1 + 0x000] $r2 // FIFO_ENABLE
206
207         // setup i0 handler, and route all interrupts to it
208         mov $r1 #ih
209         mov $iv0 $r1
210         mov $r1 0x400
211         iowr I[$r1 + 0x300] $r0 // INTR_DISPATCH
212
213         // route HUB_CHANNEL_SWITCH to fuc interrupt 8
214         mov $r3 0x404
215         shl b32 $r3 6
216         mov $r2 0x2003          // { HUB_CHANNEL_SWITCH, ZERO } -> intr 8
217         iowr I[$r3 + 0x000] $r2
218
219         // not sure what these are, route them because NVIDIA does, and
220         // the IRQ handler will signal the host if we ever get one.. we
221         // may find out if/why we need to handle these if so..
222         //
223         mov $r2 0x2004
224         iowr I[$r3 + 0x004] $r2 // { 0x04, ZERO } -> intr 9
225         mov $r2 0x200b
226         iowr I[$r3 + 0x008] $r2 // { 0x0b, ZERO } -> intr 10
227         mov $r2 0x200c
228         iowr I[$r3 + 0x01c] $r2 // { 0x0c, ZERO } -> intr 15
229
230         // enable all INTR_UP interrupts
231         mov $r2 0xc24
232         shl b32 $r2 6
233         not b32 $r3 $r0
234         iowr I[$r2] $r3
235
236         // enable fifo, ctxsw, 9, 10, 15 interrupts
237         mov $r2 -0x78fc         // 0x8704
238         sethi $r2 0
239         iowr I[$r1 + 0x000] $r2 // INTR_EN_SET
240
241         // fifo level triggered, rest edge
242         sub b32 $r1 0x100
243         mov $r2 4
244         iowr I[$r1] $r2
245
246         // enable interrupts
247         bset $flags ie0
248
249         // fetch enabled GPC/ROP counts
250         mov $r14 -0x69fc        // 0x409604
251         sethi $r14 0x400000
252         call #nv_rd32
253         extr $r1 $r15 16:20
254         st b32 D[$r0 + #rop_count] $r1
255         and $r15 0x1f
256         st b32 D[$r0 + #gpc_count] $r15
257
258         // set BAR_REQMASK to GPC mask
259         mov $r1 1
260         shl b32 $r1 $r15
261         sub b32 $r1 1
262         mov $r2 0x40c
263         shl b32 $r2 6
264         iowr I[$r2 + 0x000] $r1
265         iowr I[$r2 + 0x100] $r1
266
267         // find context data for this chipset
268         mov $r2 0x800
269         shl b32 $r2 6
270         iord $r2 I[$r2 + 0x000]         // CC_SCRATCH[0]
271         mov $r15 #chipsets - 8
272         init_find_chipset:
273                 add b32 $r15 8
274                 ld b32 $r3 D[$r15 + 0x00]
275                 cmpu b32 $r3 $r2
276                 bra e #init_context
277                 cmpu b32 $r3 0
278                 bra ne #init_find_chipset
279                 // unknown chipset
280                 ret
281
282         // context size calculation, reserve first 256 bytes for use by fuc
283         init_context:
284         mov $r1 256
285
286         // calculate size of mmio context data
287         ld b16 $r14 D[$r15 + 4]
288         ld b16 $r15 D[$r15 + 6]
289         sethi $r14 0
290         st b32 D[$r0 + #hub_mmio_list_head] $r14
291         st b32 D[$r0 + #hub_mmio_list_tail] $r15
292         call #mmctx_size
293
294         // set mmctx base addresses now so we don't have to do it later,
295         // they don't (currently) ever change
296         mov $r3 0x700
297         shl b32 $r3 6
298         shr b32 $r4 $r1 8
299         iowr I[$r3 + 0x000] $r4         // MMCTX_SAVE_SWBASE
300         iowr I[$r3 + 0x100] $r4         // MMCTX_LOAD_SWBASE
301         add b32 $r3 0x1300
302         add b32 $r1 $r15
303         shr b32 $r15 2
304         iowr I[$r3 + 0x000] $r15        // MMCTX_LOAD_COUNT, wtf for?!?
305
306         // strands, base offset needs to be aligned to 256 bytes
307         shr b32 $r1 8
308         add b32 $r1 1
309         shl b32 $r1 8
310         mov b32 $r15 $r1
311         call #strand_ctx_init
312         add b32 $r1 $r15
313
314         // initialise each GPC in sequence by passing in the offset of its
315         // context data in GPCn_CC_SCRATCH[1], and starting its FUC (which
316         // has previously been uploaded by the host) running.
317         //
318         // the GPC fuc init sequence will set GPCn_CC_SCRATCH[0] bit 31
319         // when it has completed, and return the size of its context data
320         // in GPCn_CC_SCRATCH[1]
321         //
322         ld b32 $r3 D[$r0 + #gpc_count]
323         mov $r4 0x2000
324         sethi $r4 0x500000
325         init_gpc:
326                 // setup, and start GPC ucode running
327                 add b32 $r14 $r4 0x804
328                 mov b32 $r15 $r1
329                 call #nv_wr32                   // CC_SCRATCH[1] = ctx offset
330                 add b32 $r14 $r4 0x800
331                 mov b32 $r15 $r2
332                 call #nv_wr32                   // CC_SCRATCH[0] = chipset
333                 add b32 $r14 $r4 0x10c
334                 clear b32 $r15
335                 call #nv_wr32
336                 add b32 $r14 $r4 0x104
337                 call #nv_wr32                   // ENTRY
338                 add b32 $r14 $r4 0x100
339                 mov $r15 2                      // CTRL_START_TRIGGER
340                 call #nv_wr32                   // CTRL
341
342                 // wait for it to complete, and adjust context size
343                 add b32 $r14 $r4 0x800
344                 init_gpc_wait:
345                         call #nv_rd32
346                         xbit $r15 $r15 31
347                         bra e #init_gpc_wait
348                 add b32 $r14 $r4 0x804
349                 call #nv_rd32
350                 add b32 $r1 $r15
351
352                 // next!
353                 add b32 $r4 0x8000
354                 sub b32 $r3 1
355                 bra ne #init_gpc
356
357         // save context size, and tell host we're ready
358         mov $r2 0x800
359         shl b32 $r2 6
360         iowr I[$r2 + 0x100] $r1         // CC_SCRATCH[1]  = context size
361         add b32 $r2 0x800
362         clear b32 $r1
363         bset $r1 31
364         iowr I[$r2 + 0x000] $r1         // CC_SCRATCH[0] |= 0x80000000
365
366 // Main program loop, very simple, sleeps until woken up by the interrupt
367 // handler, pulls a command from the queue and executes its handler
368 //
369 main:
370         // sleep until we have something to do
371         bset $flags $p0
372         sleep $p0
373         mov $r13 #cmd_queue
374         call #queue_get
375         bra $p1 #main
376
377         // context switch, requested by GPU?
378         cmpu b32 $r14 0x4001
379         bra ne #main_not_ctx_switch
380                 trace_set(T_AUTO)
381                 mov $r1 0xb00
382                 shl b32 $r1 6
383                 iord $r2 I[$r1 + 0x100]         // CHAN_NEXT
384                 iord $r1 I[$r1 + 0x000]         // CHAN_CUR
385
386                 xbit $r3 $r1 31
387                 bra e #chsw_no_prev
388                         xbit $r3 $r2 31
389                         bra e #chsw_prev_no_next
390                                 push $r2
391                                 mov b32 $r2 $r1
392                                 trace_set(T_SAVE)
393                                 bclr $flags $p1
394                                 bset $flags $p2
395                                 call #ctx_xfer
396                                 trace_clr(T_SAVE);
397                                 pop $r2
398                                 trace_set(T_LOAD);
399                                 bset $flags $p1
400                                 call #ctx_xfer
401                                 trace_clr(T_LOAD);
402                                 bra #chsw_done
403                         chsw_prev_no_next:
404                                 push $r2
405                                 mov b32 $r2 $r1
406                                 bclr $flags $p1
407                                 bclr $flags $p2
408                                 call #ctx_xfer
409                                 pop $r2
410                                 mov $r1 0xb00
411                                 shl b32 $r1 6
412                                 iowr I[$r1] $r2
413                                 bra #chsw_done
414                 chsw_no_prev:
415                         xbit $r3 $r2 31
416                         bra e #chsw_done
417                                 bset $flags $p1
418                                 bclr $flags $p2
419                                 call #ctx_xfer
420
421                 // ack the context switch request
422                 chsw_done:
423                 mov $r1 0xb0c
424                 shl b32 $r1 6
425                 mov $r2 1
426                 iowr I[$r1 + 0x000] $r2         // 0x409b0c
427                 trace_clr(T_AUTO)
428                 bra #main
429
430         // request to set current channel? (*not* a context switch)
431         main_not_ctx_switch:
432         cmpu b32 $r14 0x0001
433         bra ne #main_not_ctx_chan
434                 mov b32 $r2 $r15
435                 call #ctx_chan
436                 bra #main_done
437
438         // request to store current channel context?
439         main_not_ctx_chan:
440         cmpu b32 $r14 0x0002
441         bra ne #main_not_ctx_save
442                 trace_set(T_SAVE)
443                 bclr $flags $p1
444                 bclr $flags $p2
445                 call #ctx_xfer
446                 trace_clr(T_SAVE)
447                 bra #main_done
448
449         main_not_ctx_save:
450                 shl b32 $r15 $r14 16
451                 or $r15 E_BAD_COMMAND
452                 call #error
453                 bra #main
454
455         main_done:
456         mov $r1 0x820
457         shl b32 $r1 6
458         clear b32 $r2
459         bset $r2 31
460         iowr I[$r1 + 0x000] $r2         // CC_SCRATCH[0] |= 0x80000000
461         bra #main
462
463 // interrupt handler
464 ih:
465         push $r8
466         mov $r8 $flags
467         push $r8
468         push $r9
469         push $r10
470         push $r11
471         push $r13
472         push $r14
473         push $r15
474
475         // incoming fifo command?
476         iord $r10 I[$r0 + 0x200]        // INTR
477         and $r11 $r10 0x00000004
478         bra e #ih_no_fifo
479                 // queue incoming fifo command for later processing
480                 mov $r11 0x1900
481                 mov $r13 #cmd_queue
482                 iord $r14 I[$r11 + 0x100]       // FIFO_CMD
483                 iord $r15 I[$r11 + 0x000]       // FIFO_DATA
484                 call #queue_put
485                 add b32 $r11 0x400
486                 mov $r14 1
487                 iowr I[$r11 + 0x000] $r14       // FIFO_ACK
488
489         // context switch request?
490         ih_no_fifo:
491         and $r11 $r10 0x00000100
492         bra e #ih_no_ctxsw
493                 // enqueue a context switch for later processing
494                 mov $r13 #cmd_queue
495                 mov $r14 0x4001
496                 call #queue_put
497
498         // anything we didn't handle, bring it to the host's attention
499         ih_no_ctxsw:
500         mov $r11 0x104
501         not b32 $r11
502         and $r11 $r10 $r11
503         bra e #ih_no_other
504                 mov $r10 0xc1c
505                 shl b32 $r10 6
506                 iowr I[$r10] $r11       // INTR_UP_SET
507
508         // ack, and wake up main()
509         ih_no_other:
510         iowr I[$r0 + 0x100] $r10        // INTR_ACK
511
512         pop $r15
513         pop $r14
514         pop $r13
515         pop $r11
516         pop $r10
517         pop $r9
518         pop $r8
519         mov $flags $r8
520         pop $r8
521         bclr $flags $p0
522         iret
523
524 // Not real sure, but, MEM_CMD 7 will hang forever if this isn't done
525 ctx_4160s:
526         mov $r14 0x4160
527         sethi $r14 0x400000
528         mov $r15 1
529         call #nv_wr32
530         ctx_4160s_wait:
531                 call #nv_rd32
532                 xbit $r15 $r15 4
533                 bra e #ctx_4160s_wait
534         ret
535
536 // Without clearing again at end of xfer, some things cause PGRAPH
537 // to hang with STATUS=0x00000007 until it's cleared.. fbcon can
538 // still function with it set however...
539 ctx_4160c:
540         mov $r14 0x4160
541         sethi $r14 0x400000
542         clear b32 $r15
543         call #nv_wr32
544         ret
545
546 // Again, not real sure
547 //
548 // In: $r15 value to set 0x404170 to
549 //
550 ctx_4170s:
551         mov $r14 0x4170
552         sethi $r14 0x400000
553         or $r15 0x10
554         call #nv_wr32
555         ret
556
557 // Waits for a ctx_4170s() call to complete
558 //
559 ctx_4170w:
560         mov $r14 0x4170
561         sethi $r14 0x400000
562         call #nv_rd32
563         and $r15 0x10
564         bra ne #ctx_4170w
565         ret
566
567 // Disables various things, waits a bit, and re-enables them..
568 //
569 // Not sure how exactly this helps, perhaps "ENABLE" is not such a
570 // good description for the bits we turn off?  Anyways, without this,
571 // funny things happen.
572 //
573 ctx_redswitch:
574         mov $r14 0x614
575         shl b32 $r14 6
576         mov $r15 0x270
577         iowr I[$r14] $r15       // HUB_RED_SWITCH = ENABLE_GPC, POWER_ALL
578         mov $r15 8
579         ctx_redswitch_delay:
580                 sub b32 $r15 1
581                 bra ne #ctx_redswitch_delay
582         mov $r15 0x770
583         iowr I[$r14] $r15       // HUB_RED_SWITCH = ENABLE_ALL, POWER_ALL
584         ret
585
586 // Not a clue what this is for, except that unless the value is 0x10, the
587 // strand context is saved (and presumably restored) incorrectly..
588 //
589 // In: $r15 value to set to (0x00/0x10 are used)
590 //
591 ctx_86c:
592         mov $r14 0x86c
593         shl b32 $r14 6
594         iowr I[$r14] $r15       // HUB(0x86c) = val
595         mov $r14 -0x75ec
596         sethi $r14 0x400000
597         call #nv_wr32           // ROP(0xa14) = val
598         mov $r14 -0x5794
599         sethi $r14 0x410000
600         call #nv_wr32           // GPC(0x86c) = val
601         ret
602
603 // ctx_load - load's a channel's ctxctl data, and selects its vm
604 //
605 // In: $r2 channel address
606 //
607 ctx_load:
608         trace_set(T_CHAN)
609
610         // switch to channel, somewhat magic in parts..
611         mov $r10 12             // DONE_UNK12
612         call #wait_donez
613         mov $r1 0xa24
614         shl b32 $r1 6
615         iowr I[$r1 + 0x000] $r0 // 0x409a24
616         mov $r3 0xb00
617         shl b32 $r3 6
618         iowr I[$r3 + 0x100] $r2 // CHAN_NEXT
619         mov $r1 0xa0c
620         shl b32 $r1 6
621         mov $r4 7
622         iowr I[$r1 + 0x000] $r2 // MEM_CHAN
623         iowr I[$r1 + 0x100] $r4 // MEM_CMD
624         ctx_chan_wait_0:
625                 iord $r4 I[$r1 + 0x100]
626                 and $r4 0x1f
627                 bra ne #ctx_chan_wait_0
628         iowr I[$r3 + 0x000] $r2 // CHAN_CUR
629
630         // load channel header, fetch PGRAPH context pointer
631         mov $xtargets $r0
632         bclr $r2 31
633         shl b32 $r2 4
634         add b32 $r2 2
635
636         trace_set(T_LCHAN)
637         mov $r1 0xa04
638         shl b32 $r1 6
639         iowr I[$r1 + 0x000] $r2         // MEM_BASE
640         mov $r1 0xa20
641         shl b32 $r1 6
642         mov $r2 0x0002
643         sethi $r2 0x80000000
644         iowr I[$r1 + 0x000] $r2         // MEM_TARGET = vram
645         mov $r1 0x10                    // chan + 0x0210
646         mov $r2 #xfer_data
647         sethi $r2 0x00020000            // 16 bytes
648         xdld $r1 $r2
649         xdwait
650         trace_clr(T_LCHAN)
651
652         // update current context
653         ld b32 $r1 D[$r0 + #xfer_data + 4]
654         shl b32 $r1 24
655         ld b32 $r2 D[$r0 + #xfer_data + 0]
656         shr b32 $r2 8
657         or $r1 $r2
658         st b32 D[$r0 + #ctx_current] $r1
659
660         // set transfer base to start of context, and fetch context header
661         trace_set(T_LCTXH)
662         mov $r2 0xa04
663         shl b32 $r2 6
664         iowr I[$r2 + 0x000] $r1         // MEM_BASE
665         mov $r2 1
666         mov $r1 0xa20
667         shl b32 $r1 6
668         iowr I[$r1 + 0x000] $r2         // MEM_TARGET = vm
669         mov $r1 #chan_data
670         sethi $r1 0x00060000            // 256 bytes
671         xdld $r0 $r1
672         xdwait
673         trace_clr(T_LCTXH)
674
675         trace_clr(T_CHAN)
676         ret
677
678 // ctx_chan - handler for HUB_SET_CHAN command, will set a channel as
679 //            the active channel for ctxctl, but not actually transfer
680 //            any context data.  intended for use only during initial
681 //            context construction.
682 //
683 // In: $r2 channel address
684 //
685 ctx_chan:
686         call #ctx_4160s
687         call #ctx_load
688         mov $r10 12                     // DONE_UNK12
689         call #wait_donez
690         mov $r1 0xa10
691         shl b32 $r1 6
692         mov $r2 5
693         iowr I[$r1 + 0x000] $r2         // MEM_CMD = 5 (???)
694         ctx_chan_wait:
695                 iord $r2 I[$r1 + 0x000]
696                 or $r2 $r2
697                 bra ne #ctx_chan_wait
698         call #ctx_4160c
699         ret
700
701 // Execute per-context state overrides list
702 //
703 // Only executed on the first load of a channel.  Might want to look into
704 // removing this and having the host directly modify the channel's context
705 // to change this state...  The nouveau DRM already builds this list as
706 // it's definitely needed for NVIDIA's, so we may as well use it for now
707 //
708 // Input: $r1 mmio list length
709 //
710 ctx_mmio_exec:
711         // set transfer base to be the mmio list
712         ld b32 $r3 D[$r0 + #chan_mmio_address]
713         mov $r2 0xa04
714         shl b32 $r2 6
715         iowr I[$r2 + 0x000] $r3         // MEM_BASE
716
717         clear b32 $r3
718         ctx_mmio_loop:
719                 // fetch next 256 bytes of mmio list if necessary
720                 and $r4 $r3 0xff
721                 bra ne #ctx_mmio_pull
722                         mov $r5 #xfer_data
723                         sethi $r5 0x00060000    // 256 bytes
724                         xdld $r3 $r5
725                         xdwait
726
727                 // execute a single list entry
728                 ctx_mmio_pull:
729                 ld b32 $r14 D[$r4 + #xfer_data + 0x00]
730                 ld b32 $r15 D[$r4 + #xfer_data + 0x04]
731                 call #nv_wr32
732
733                 // next!
734                 add b32 $r3 8
735                 sub b32 $r1 1
736                 bra ne #ctx_mmio_loop
737
738         // set transfer base back to the current context
739         ctx_mmio_done:
740         ld b32 $r3 D[$r0 + #ctx_current]
741         iowr I[$r2 + 0x000] $r3         // MEM_BASE
742
743         // disable the mmio list now, we don't need/want to execute it again
744         st b32 D[$r0 + #chan_mmio_count] $r0
745         mov $r1 #chan_data
746         sethi $r1 0x00060000            // 256 bytes
747         xdst $r0 $r1
748         xdwait
749         ret
750
751 // Transfer HUB context data between GPU and storage area
752 //
753 // In: $r2 channel address
754 //     $p1 clear on save, set on load
755 //     $p2 set if opposite direction done/will be done, so:
756 //              on save it means: "a load will follow this save"
757 //              on load it means: "a save preceeded this load"
758 //
759 ctx_xfer:
760         // according to mwk, some kind of wait for idle
761         mov $r15 0xc00
762         shl b32 $r15 6
763         mov $r14 4
764         iowr I[$r15 + 0x200] $r14
765         ctx_xfer_idle:
766                 iord $r14 I[$r15 + 0x000]
767                 and $r14 0x2000
768                 bra ne #ctx_xfer_idle
769
770         bra not $p1 #ctx_xfer_pre
771         bra $p2 #ctx_xfer_pre_load
772         ctx_xfer_pre:
773                 mov $r15 0x10
774                 call #ctx_86c
775                 call #ctx_4160s
776                 bra not $p1 #ctx_xfer_exec
777
778         ctx_xfer_pre_load:
779                 mov $r15 2
780                 call #ctx_4170s
781                 call #ctx_4170w
782                 call #ctx_redswitch
783                 clear b32 $r15
784                 call #ctx_4170s
785                 call #ctx_load
786
787         // fetch context pointer, and initiate xfer on all GPCs
788         ctx_xfer_exec:
789         ld b32 $r1 D[$r0 + #ctx_current]
790         mov $r2 0x414
791         shl b32 $r2 6
792         iowr I[$r2 + 0x000] $r0 // BAR_STATUS = reset
793         mov $r14 -0x5b00
794         sethi $r14 0x410000
795         mov b32 $r15 $r1
796         call #nv_wr32           // GPC_BCAST_WRCMD_DATA = ctx pointer
797         add b32 $r14 4
798         xbit $r15 $flags $p1
799         xbit $r2 $flags $p2
800         shl b32 $r2 1
801         or $r15 $r2
802         call #nv_wr32           // GPC_BCAST_WRCMD_CMD = GPC_XFER(type)
803
804         // strands
805         mov $r1 0x4afc
806         sethi $r1 0x20000
807         mov $r2 0xc
808         iowr I[$r1] $r2         // STRAND_CMD(0x3f) = 0x0c
809         call #strand_wait
810         mov $r2 0x47fc
811         sethi $r2 0x20000
812         iowr I[$r2] $r0         // STRAND_FIRST_GENE(0x3f) = 0x00
813         xbit $r2 $flags $p1
814         add b32 $r2 3
815         iowr I[$r1] $r2         // STRAND_CMD(0x3f) = 0x03/0x04 (SAVE/LOAD)
816
817         // mmio context
818         xbit $r10 $flags $p1    // direction
819         or $r10 6               // first, last
820         mov $r11 0              // base = 0
821         ld b32 $r12 D[$r0 + #hub_mmio_list_head]
822         ld b32 $r13 D[$r0 + #hub_mmio_list_tail]
823         mov $r14 0              // not multi
824         call #mmctx_xfer
825
826         // wait for GPCs to all complete
827         mov $r10 8              // DONE_BAR
828         call #wait_doneo
829
830         // wait for strand xfer to complete
831         call #strand_wait
832
833         // post-op
834         bra $p1 #ctx_xfer_post
835                 mov $r10 12             // DONE_UNK12
836                 call #wait_donez
837                 mov $r1 0xa10
838                 shl b32 $r1 6
839                 mov $r2 5
840                 iowr I[$r1] $r2         // MEM_CMD
841                 ctx_xfer_post_save_wait:
842                         iord $r2 I[$r1]
843                         or $r2 $r2
844                         bra ne #ctx_xfer_post_save_wait
845
846         bra $p2 #ctx_xfer_done
847         ctx_xfer_post:
848                 mov $r15 2
849                 call #ctx_4170s
850                 clear b32 $r15
851                 call #ctx_86c
852                 call #strand_post
853                 call #ctx_4170w
854                 clear b32 $r15
855                 call #ctx_4170s
856
857                 bra not $p1 #ctx_xfer_no_post_mmio
858                 ld b32 $r1 D[$r0 + #chan_mmio_count]
859                 or $r1 $r1
860                 bra e #ctx_xfer_no_post_mmio
861                         call #ctx_mmio_exec
862
863                 ctx_xfer_no_post_mmio:
864                 call #ctx_4160c
865
866         ctx_xfer_done:
867         ret
868
869 .align 256