ctxnv40.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * Copyright 2009 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. /* NVIDIA context programs handle a number of other conditions which are
  25. * not implemented in our versions. It's not clear why NVIDIA context
  26. * programs have this code, nor whether it's strictly necessary for
  27. * correct operation. We'll implement additional handling if/when we
  28. * discover it's necessary.
  29. *
  30. * - On context save, NVIDIA set 0x400314 bit 0 to 1 if the "3D state"
  31. * flag is set, this gets saved into the context.
  32. * - On context save, the context program for all cards load nsource
  33. * into a flag register and check for ILLEGAL_MTHD. If it's set,
  34. * opcode 0x60000d is called before resuming normal operation.
  35. * - Some context programs check more conditions than the above. NV44
  36. * checks: ((nsource & 0x0857) || (0x400718 & 0x0100) || (intr & 0x0001))
  37. * and calls 0x60000d before resuming normal operation.
  38. * - At the very beginning of NVIDIA's context programs, flag 9 is checked
  39. * and if true 0x800001 is called with count=0, pos=0, the flag is cleared
  40. * and then the ctxprog is aborted. It looks like a complicated NOP,
  41. * its purpose is unknown.
  42. * - In the section of code that loads the per-vs state, NVIDIA check
  43. * flag 10. If it's set, they only transfer the small 0x300 byte block
  44. * of state + the state for a single vs as opposed to the state for
  45. * all vs units. It doesn't seem likely that it'll occur in normal
  46. * operation, especially seeing as it appears NVIDIA may have screwed
  47. * up the ctxprogs for some cards and have an invalid instruction
  48. * rather than a cp_lsr(ctx, dwords_for_1_vs_unit) instruction.
  49. * - There's a number of places where context offset 0 (where we place
  50. * the PRAMIN offset of the context) is loaded into either 0x408000,
  51. * 0x408004 or 0x408008. Not sure what's up there either.
  52. * - The ctxprogs for some cards save 0x400a00 again during the cleanup
  53. * path for auto-loadctx.
  54. */
  55. #define CP_FLAG_CLEAR 0
  56. #define CP_FLAG_SET 1
  57. #define CP_FLAG_SWAP_DIRECTION ((0 * 32) + 0)
  58. #define CP_FLAG_SWAP_DIRECTION_LOAD 0
  59. #define CP_FLAG_SWAP_DIRECTION_SAVE 1
  60. #define CP_FLAG_USER_SAVE ((0 * 32) + 5)
  61. #define CP_FLAG_USER_SAVE_NOT_PENDING 0
  62. #define CP_FLAG_USER_SAVE_PENDING 1
  63. #define CP_FLAG_USER_LOAD ((0 * 32) + 6)
  64. #define CP_FLAG_USER_LOAD_NOT_PENDING 0
  65. #define CP_FLAG_USER_LOAD_PENDING 1
  66. #define CP_FLAG_STATUS ((3 * 32) + 0)
  67. #define CP_FLAG_STATUS_IDLE 0
  68. #define CP_FLAG_STATUS_BUSY 1
  69. #define CP_FLAG_AUTO_SAVE ((3 * 32) + 4)
  70. #define CP_FLAG_AUTO_SAVE_NOT_PENDING 0
  71. #define CP_FLAG_AUTO_SAVE_PENDING 1
  72. #define CP_FLAG_AUTO_LOAD ((3 * 32) + 5)
  73. #define CP_FLAG_AUTO_LOAD_NOT_PENDING 0
  74. #define CP_FLAG_AUTO_LOAD_PENDING 1
  75. #define CP_FLAG_UNK54 ((3 * 32) + 6)
  76. #define CP_FLAG_UNK54_CLEAR 0
  77. #define CP_FLAG_UNK54_SET 1
  78. #define CP_FLAG_ALWAYS ((3 * 32) + 8)
  79. #define CP_FLAG_ALWAYS_FALSE 0
  80. #define CP_FLAG_ALWAYS_TRUE 1
  81. #define CP_FLAG_UNK57 ((3 * 32) + 9)
  82. #define CP_FLAG_UNK57_CLEAR 0
  83. #define CP_FLAG_UNK57_SET 1
  84. #define CP_CTX 0x00100000
  85. #define CP_CTX_COUNT 0x000fc000
  86. #define CP_CTX_COUNT_SHIFT 14
  87. #define CP_CTX_REG 0x00003fff
  88. #define CP_LOAD_SR 0x00200000
  89. #define CP_LOAD_SR_VALUE 0x000fffff
  90. #define CP_BRA 0x00400000
  91. #define CP_BRA_IP 0x0000ff00
  92. #define CP_BRA_IP_SHIFT 8
  93. #define CP_BRA_IF_CLEAR 0x00000080
  94. #define CP_BRA_FLAG 0x0000007f
  95. #define CP_WAIT 0x00500000
  96. #define CP_WAIT_SET 0x00000080
  97. #define CP_WAIT_FLAG 0x0000007f
  98. #define CP_SET 0x00700000
  99. #define CP_SET_1 0x00000080
  100. #define CP_SET_FLAG 0x0000007f
  101. #define CP_NEXT_TO_SWAP 0x00600007
  102. #define CP_NEXT_TO_CURRENT 0x00600009
  103. #define CP_SET_CONTEXT_POINTER 0x0060000a
  104. #define CP_END 0x0060000e
  105. #define CP_LOAD_MAGIC_UNK01 0x00800001 /* unknown */
  106. #define CP_LOAD_MAGIC_NV44TCL 0x00800029 /* per-vs state (0x4497) */
  107. #define CP_LOAD_MAGIC_NV40TCL 0x00800041 /* per-vs state (0x4097) */
  108. #include "ctxnv40.h"
  109. #include "nv40.h"
  110. /* TODO:
  111. * - get vs count from 0x1540
  112. */
  113. static int
  114. nv40_gr_vs_count(struct nvkm_device *device)
  115. {
  116. switch (device->chipset) {
  117. case 0x47:
  118. case 0x49:
  119. case 0x4b:
  120. return 8;
  121. case 0x40:
  122. return 6;
  123. case 0x41:
  124. case 0x42:
  125. return 5;
  126. case 0x43:
  127. case 0x44:
  128. case 0x46:
  129. case 0x4a:
  130. return 3;
  131. case 0x4c:
  132. case 0x4e:
  133. case 0x67:
  134. default:
  135. return 1;
  136. }
  137. }
  138. enum cp_label {
  139. cp_check_load = 1,
  140. cp_setup_auto_load,
  141. cp_setup_load,
  142. cp_setup_save,
  143. cp_swap_state,
  144. cp_swap_state3d_3_is_save,
  145. cp_prepare_exit,
  146. cp_exit,
  147. };
  148. static void
  149. nv40_gr_construct_general(struct nvkm_grctx *ctx)
  150. {
  151. struct nvkm_device *device = ctx->device;
  152. int i;
  153. cp_ctx(ctx, 0x4000a4, 1);
  154. gr_def(ctx, 0x4000a4, 0x00000008);
  155. cp_ctx(ctx, 0x400144, 58);
  156. gr_def(ctx, 0x400144, 0x00000001);
  157. cp_ctx(ctx, 0x400314, 1);
  158. gr_def(ctx, 0x400314, 0x00000000);
  159. cp_ctx(ctx, 0x400400, 10);
  160. cp_ctx(ctx, 0x400480, 10);
  161. cp_ctx(ctx, 0x400500, 19);
  162. gr_def(ctx, 0x400514, 0x00040000);
  163. gr_def(ctx, 0x400524, 0x55555555);
  164. gr_def(ctx, 0x400528, 0x55555555);
  165. gr_def(ctx, 0x40052c, 0x55555555);
  166. gr_def(ctx, 0x400530, 0x55555555);
  167. cp_ctx(ctx, 0x400560, 6);
  168. gr_def(ctx, 0x400568, 0x0000ffff);
  169. gr_def(ctx, 0x40056c, 0x0000ffff);
  170. cp_ctx(ctx, 0x40057c, 5);
  171. cp_ctx(ctx, 0x400710, 3);
  172. gr_def(ctx, 0x400710, 0x20010001);
  173. gr_def(ctx, 0x400714, 0x0f73ef00);
  174. cp_ctx(ctx, 0x400724, 1);
  175. gr_def(ctx, 0x400724, 0x02008821);
  176. cp_ctx(ctx, 0x400770, 3);
  177. if (device->chipset == 0x40) {
  178. cp_ctx(ctx, 0x400814, 4);
  179. cp_ctx(ctx, 0x400828, 5);
  180. cp_ctx(ctx, 0x400840, 5);
  181. gr_def(ctx, 0x400850, 0x00000040);
  182. cp_ctx(ctx, 0x400858, 4);
  183. gr_def(ctx, 0x400858, 0x00000040);
  184. gr_def(ctx, 0x40085c, 0x00000040);
  185. gr_def(ctx, 0x400864, 0x80000000);
  186. cp_ctx(ctx, 0x40086c, 9);
  187. gr_def(ctx, 0x40086c, 0x80000000);
  188. gr_def(ctx, 0x400870, 0x80000000);
  189. gr_def(ctx, 0x400874, 0x80000000);
  190. gr_def(ctx, 0x400878, 0x80000000);
  191. gr_def(ctx, 0x400888, 0x00000040);
  192. gr_def(ctx, 0x40088c, 0x80000000);
  193. cp_ctx(ctx, 0x4009c0, 8);
  194. gr_def(ctx, 0x4009cc, 0x80000000);
  195. gr_def(ctx, 0x4009dc, 0x80000000);
  196. } else {
  197. cp_ctx(ctx, 0x400840, 20);
  198. if (nv44_gr_class(ctx->device)) {
  199. for (i = 0; i < 8; i++)
  200. gr_def(ctx, 0x400860 + (i * 4), 0x00000001);
  201. }
  202. gr_def(ctx, 0x400880, 0x00000040);
  203. gr_def(ctx, 0x400884, 0x00000040);
  204. gr_def(ctx, 0x400888, 0x00000040);
  205. cp_ctx(ctx, 0x400894, 11);
  206. gr_def(ctx, 0x400894, 0x00000040);
  207. if (!nv44_gr_class(ctx->device)) {
  208. for (i = 0; i < 8; i++)
  209. gr_def(ctx, 0x4008a0 + (i * 4), 0x80000000);
  210. }
  211. cp_ctx(ctx, 0x4008e0, 2);
  212. cp_ctx(ctx, 0x4008f8, 2);
  213. if (device->chipset == 0x4c ||
  214. (device->chipset & 0xf0) == 0x60)
  215. cp_ctx(ctx, 0x4009f8, 1);
  216. }
  217. cp_ctx(ctx, 0x400a00, 73);
  218. gr_def(ctx, 0x400b0c, 0x0b0b0b0c);
  219. cp_ctx(ctx, 0x401000, 4);
  220. cp_ctx(ctx, 0x405004, 1);
  221. switch (device->chipset) {
  222. case 0x47:
  223. case 0x49:
  224. case 0x4b:
  225. cp_ctx(ctx, 0x403448, 1);
  226. gr_def(ctx, 0x403448, 0x00001010);
  227. break;
  228. default:
  229. cp_ctx(ctx, 0x403440, 1);
  230. switch (device->chipset) {
  231. case 0x40:
  232. gr_def(ctx, 0x403440, 0x00000010);
  233. break;
  234. case 0x44:
  235. case 0x46:
  236. case 0x4a:
  237. gr_def(ctx, 0x403440, 0x00003010);
  238. break;
  239. case 0x41:
  240. case 0x42:
  241. case 0x43:
  242. case 0x4c:
  243. case 0x4e:
  244. case 0x67:
  245. default:
  246. gr_def(ctx, 0x403440, 0x00001010);
  247. break;
  248. }
  249. break;
  250. }
  251. }
  252. static void
  253. nv40_gr_construct_state3d(struct nvkm_grctx *ctx)
  254. {
  255. struct nvkm_device *device = ctx->device;
  256. int i;
  257. if (device->chipset == 0x40) {
  258. cp_ctx(ctx, 0x401880, 51);
  259. gr_def(ctx, 0x401940, 0x00000100);
  260. } else
  261. if (device->chipset == 0x46 || device->chipset == 0x47 ||
  262. device->chipset == 0x49 || device->chipset == 0x4b) {
  263. cp_ctx(ctx, 0x401880, 32);
  264. for (i = 0; i < 16; i++)
  265. gr_def(ctx, 0x401880 + (i * 4), 0x00000111);
  266. if (device->chipset == 0x46)
  267. cp_ctx(ctx, 0x401900, 16);
  268. cp_ctx(ctx, 0x401940, 3);
  269. }
  270. cp_ctx(ctx, 0x40194c, 18);
  271. gr_def(ctx, 0x401954, 0x00000111);
  272. gr_def(ctx, 0x401958, 0x00080060);
  273. gr_def(ctx, 0x401974, 0x00000080);
  274. gr_def(ctx, 0x401978, 0xffff0000);
  275. gr_def(ctx, 0x40197c, 0x00000001);
  276. gr_def(ctx, 0x401990, 0x46400000);
  277. if (device->chipset == 0x40) {
  278. cp_ctx(ctx, 0x4019a0, 2);
  279. cp_ctx(ctx, 0x4019ac, 5);
  280. } else {
  281. cp_ctx(ctx, 0x4019a0, 1);
  282. cp_ctx(ctx, 0x4019b4, 3);
  283. }
  284. gr_def(ctx, 0x4019bc, 0xffff0000);
  285. switch (device->chipset) {
  286. case 0x46:
  287. case 0x47:
  288. case 0x49:
  289. case 0x4b:
  290. cp_ctx(ctx, 0x4019c0, 18);
  291. for (i = 0; i < 16; i++)
  292. gr_def(ctx, 0x4019c0 + (i * 4), 0x88888888);
  293. break;
  294. }
  295. cp_ctx(ctx, 0x401a08, 8);
  296. gr_def(ctx, 0x401a10, 0x0fff0000);
  297. gr_def(ctx, 0x401a14, 0x0fff0000);
  298. gr_def(ctx, 0x401a1c, 0x00011100);
  299. cp_ctx(ctx, 0x401a2c, 4);
  300. cp_ctx(ctx, 0x401a44, 26);
  301. for (i = 0; i < 16; i++)
  302. gr_def(ctx, 0x401a44 + (i * 4), 0x07ff0000);
  303. gr_def(ctx, 0x401a8c, 0x4b7fffff);
  304. if (device->chipset == 0x40) {
  305. cp_ctx(ctx, 0x401ab8, 3);
  306. } else {
  307. cp_ctx(ctx, 0x401ab8, 1);
  308. cp_ctx(ctx, 0x401ac0, 1);
  309. }
  310. cp_ctx(ctx, 0x401ad0, 8);
  311. gr_def(ctx, 0x401ad0, 0x30201000);
  312. gr_def(ctx, 0x401ad4, 0x70605040);
  313. gr_def(ctx, 0x401ad8, 0xb8a89888);
  314. gr_def(ctx, 0x401adc, 0xf8e8d8c8);
  315. cp_ctx(ctx, 0x401b10, device->chipset == 0x40 ? 2 : 1);
  316. gr_def(ctx, 0x401b10, 0x40100000);
  317. cp_ctx(ctx, 0x401b18, device->chipset == 0x40 ? 6 : 5);
  318. gr_def(ctx, 0x401b28, device->chipset == 0x40 ?
  319. 0x00000004 : 0x00000000);
  320. cp_ctx(ctx, 0x401b30, 25);
  321. gr_def(ctx, 0x401b34, 0x0000ffff);
  322. gr_def(ctx, 0x401b68, 0x435185d6);
  323. gr_def(ctx, 0x401b6c, 0x2155b699);
  324. gr_def(ctx, 0x401b70, 0xfedcba98);
  325. gr_def(ctx, 0x401b74, 0x00000098);
  326. gr_def(ctx, 0x401b84, 0xffffffff);
  327. gr_def(ctx, 0x401b88, 0x00ff7000);
  328. gr_def(ctx, 0x401b8c, 0x0000ffff);
  329. if (device->chipset != 0x44 && device->chipset != 0x4a &&
  330. device->chipset != 0x4e)
  331. cp_ctx(ctx, 0x401b94, 1);
  332. cp_ctx(ctx, 0x401b98, 8);
  333. gr_def(ctx, 0x401b9c, 0x00ff0000);
  334. cp_ctx(ctx, 0x401bc0, 9);
  335. gr_def(ctx, 0x401be0, 0x00ffff00);
  336. cp_ctx(ctx, 0x401c00, 192);
  337. for (i = 0; i < 16; i++) { /* fragment texture units */
  338. gr_def(ctx, 0x401c40 + (i * 4), 0x00018488);
  339. gr_def(ctx, 0x401c80 + (i * 4), 0x00028202);
  340. gr_def(ctx, 0x401d00 + (i * 4), 0x0000aae4);
  341. gr_def(ctx, 0x401d40 + (i * 4), 0x01012000);
  342. gr_def(ctx, 0x401d80 + (i * 4), 0x00080008);
  343. gr_def(ctx, 0x401e00 + (i * 4), 0x00100008);
  344. }
  345. for (i = 0; i < 4; i++) { /* vertex texture units */
  346. gr_def(ctx, 0x401e90 + (i * 4), 0x0001bc80);
  347. gr_def(ctx, 0x401ea0 + (i * 4), 0x00000202);
  348. gr_def(ctx, 0x401ec0 + (i * 4), 0x00000008);
  349. gr_def(ctx, 0x401ee0 + (i * 4), 0x00080008);
  350. }
  351. cp_ctx(ctx, 0x400f5c, 3);
  352. gr_def(ctx, 0x400f5c, 0x00000002);
  353. cp_ctx(ctx, 0x400f84, 1);
  354. }
  355. static void
  356. nv40_gr_construct_state3d_2(struct nvkm_grctx *ctx)
  357. {
  358. struct nvkm_device *device = ctx->device;
  359. int i;
  360. cp_ctx(ctx, 0x402000, 1);
  361. cp_ctx(ctx, 0x402404, device->chipset == 0x40 ? 1 : 2);
  362. switch (device->chipset) {
  363. case 0x40:
  364. gr_def(ctx, 0x402404, 0x00000001);
  365. break;
  366. case 0x4c:
  367. case 0x4e:
  368. case 0x67:
  369. gr_def(ctx, 0x402404, 0x00000020);
  370. break;
  371. case 0x46:
  372. case 0x49:
  373. case 0x4b:
  374. gr_def(ctx, 0x402404, 0x00000421);
  375. break;
  376. default:
  377. gr_def(ctx, 0x402404, 0x00000021);
  378. }
  379. if (device->chipset != 0x40)
  380. gr_def(ctx, 0x402408, 0x030c30c3);
  381. switch (device->chipset) {
  382. case 0x44:
  383. case 0x46:
  384. case 0x4a:
  385. case 0x4c:
  386. case 0x4e:
  387. case 0x67:
  388. cp_ctx(ctx, 0x402440, 1);
  389. gr_def(ctx, 0x402440, 0x00011001);
  390. break;
  391. default:
  392. break;
  393. }
  394. cp_ctx(ctx, 0x402480, device->chipset == 0x40 ? 8 : 9);
  395. gr_def(ctx, 0x402488, 0x3e020200);
  396. gr_def(ctx, 0x40248c, 0x00ffffff);
  397. switch (device->chipset) {
  398. case 0x40:
  399. gr_def(ctx, 0x402490, 0x60103f00);
  400. break;
  401. case 0x47:
  402. gr_def(ctx, 0x402490, 0x40103f00);
  403. break;
  404. case 0x41:
  405. case 0x42:
  406. case 0x49:
  407. case 0x4b:
  408. gr_def(ctx, 0x402490, 0x20103f00);
  409. break;
  410. default:
  411. gr_def(ctx, 0x402490, 0x0c103f00);
  412. break;
  413. }
  414. gr_def(ctx, 0x40249c, device->chipset <= 0x43 ?
  415. 0x00020000 : 0x00040000);
  416. cp_ctx(ctx, 0x402500, 31);
  417. gr_def(ctx, 0x402530, 0x00008100);
  418. if (device->chipset == 0x40)
  419. cp_ctx(ctx, 0x40257c, 6);
  420. cp_ctx(ctx, 0x402594, 16);
  421. cp_ctx(ctx, 0x402800, 17);
  422. gr_def(ctx, 0x402800, 0x00000001);
  423. switch (device->chipset) {
  424. case 0x47:
  425. case 0x49:
  426. case 0x4b:
  427. cp_ctx(ctx, 0x402864, 1);
  428. gr_def(ctx, 0x402864, 0x00001001);
  429. cp_ctx(ctx, 0x402870, 3);
  430. gr_def(ctx, 0x402878, 0x00000003);
  431. if (device->chipset != 0x47) { /* belong at end!! */
  432. cp_ctx(ctx, 0x402900, 1);
  433. cp_ctx(ctx, 0x402940, 1);
  434. cp_ctx(ctx, 0x402980, 1);
  435. cp_ctx(ctx, 0x4029c0, 1);
  436. cp_ctx(ctx, 0x402a00, 1);
  437. cp_ctx(ctx, 0x402a40, 1);
  438. cp_ctx(ctx, 0x402a80, 1);
  439. cp_ctx(ctx, 0x402ac0, 1);
  440. }
  441. break;
  442. case 0x40:
  443. cp_ctx(ctx, 0x402844, 1);
  444. gr_def(ctx, 0x402844, 0x00000001);
  445. cp_ctx(ctx, 0x402850, 1);
  446. break;
  447. default:
  448. cp_ctx(ctx, 0x402844, 1);
  449. gr_def(ctx, 0x402844, 0x00001001);
  450. cp_ctx(ctx, 0x402850, 2);
  451. gr_def(ctx, 0x402854, 0x00000003);
  452. break;
  453. }
  454. cp_ctx(ctx, 0x402c00, 4);
  455. gr_def(ctx, 0x402c00, device->chipset == 0x40 ?
  456. 0x80800001 : 0x00888001);
  457. switch (device->chipset) {
  458. case 0x47:
  459. case 0x49:
  460. case 0x4b:
  461. cp_ctx(ctx, 0x402c20, 40);
  462. for (i = 0; i < 32; i++)
  463. gr_def(ctx, 0x402c40 + (i * 4), 0xffffffff);
  464. cp_ctx(ctx, 0x4030b8, 13);
  465. gr_def(ctx, 0x4030dc, 0x00000005);
  466. gr_def(ctx, 0x4030e8, 0x0000ffff);
  467. break;
  468. default:
  469. cp_ctx(ctx, 0x402c10, 4);
  470. if (device->chipset == 0x40)
  471. cp_ctx(ctx, 0x402c20, 36);
  472. else
  473. if (device->chipset <= 0x42)
  474. cp_ctx(ctx, 0x402c20, 24);
  475. else
  476. if (device->chipset <= 0x4a)
  477. cp_ctx(ctx, 0x402c20, 16);
  478. else
  479. cp_ctx(ctx, 0x402c20, 8);
  480. cp_ctx(ctx, 0x402cb0, device->chipset == 0x40 ? 12 : 13);
  481. gr_def(ctx, 0x402cd4, 0x00000005);
  482. if (device->chipset != 0x40)
  483. gr_def(ctx, 0x402ce0, 0x0000ffff);
  484. break;
  485. }
  486. cp_ctx(ctx, 0x403400, device->chipset == 0x40 ? 4 : 3);
  487. cp_ctx(ctx, 0x403410, device->chipset == 0x40 ? 4 : 3);
  488. cp_ctx(ctx, 0x403420, nv40_gr_vs_count(ctx->device));
  489. for (i = 0; i < nv40_gr_vs_count(ctx->device); i++)
  490. gr_def(ctx, 0x403420 + (i * 4), 0x00005555);
  491. if (device->chipset != 0x40) {
  492. cp_ctx(ctx, 0x403600, 1);
  493. gr_def(ctx, 0x403600, 0x00000001);
  494. }
  495. cp_ctx(ctx, 0x403800, 1);
  496. cp_ctx(ctx, 0x403c18, 1);
  497. gr_def(ctx, 0x403c18, 0x00000001);
  498. switch (device->chipset) {
  499. case 0x46:
  500. case 0x47:
  501. case 0x49:
  502. case 0x4b:
  503. cp_ctx(ctx, 0x405018, 1);
  504. gr_def(ctx, 0x405018, 0x08e00001);
  505. cp_ctx(ctx, 0x405c24, 1);
  506. gr_def(ctx, 0x405c24, 0x000e3000);
  507. break;
  508. }
  509. if (device->chipset != 0x4e)
  510. cp_ctx(ctx, 0x405800, 11);
  511. cp_ctx(ctx, 0x407000, 1);
  512. }
  513. static void
  514. nv40_gr_construct_state3d_3(struct nvkm_grctx *ctx)
  515. {
  516. int len = nv44_gr_class(ctx->device) ? 0x0084 : 0x0684;
  517. cp_out (ctx, 0x300000);
  518. cp_lsr (ctx, len - 4);
  519. cp_bra (ctx, SWAP_DIRECTION, SAVE, cp_swap_state3d_3_is_save);
  520. cp_lsr (ctx, len);
  521. cp_name(ctx, cp_swap_state3d_3_is_save);
  522. cp_out (ctx, 0x800001);
  523. ctx->ctxvals_pos += len;
  524. }
  525. static void
  526. nv40_gr_construct_shader(struct nvkm_grctx *ctx)
  527. {
  528. struct nvkm_device *device = ctx->device;
  529. struct nvkm_gpuobj *obj = ctx->data;
  530. int vs, vs_nr, vs_len, vs_nr_b0, vs_nr_b1, b0_offset, b1_offset;
  531. int offset, i;
  532. vs_nr = nv40_gr_vs_count(ctx->device);
  533. vs_nr_b0 = 363;
  534. vs_nr_b1 = device->chipset == 0x40 ? 128 : 64;
  535. if (device->chipset == 0x40) {
  536. b0_offset = 0x2200/4; /* 33a0 */
  537. b1_offset = 0x55a0/4; /* 1500 */
  538. vs_len = 0x6aa0/4;
  539. } else
  540. if (device->chipset == 0x41 || device->chipset == 0x42) {
  541. b0_offset = 0x2200/4; /* 2200 */
  542. b1_offset = 0x4400/4; /* 0b00 */
  543. vs_len = 0x4f00/4;
  544. } else {
  545. b0_offset = 0x1d40/4; /* 2200 */
  546. b1_offset = 0x3f40/4; /* 0b00 : 0a40 */
  547. vs_len = nv44_gr_class(device) ? 0x4980/4 : 0x4a40/4;
  548. }
  549. cp_lsr(ctx, vs_len * vs_nr + 0x300/4);
  550. cp_out(ctx, nv44_gr_class(device) ? 0x800029 : 0x800041);
  551. offset = ctx->ctxvals_pos;
  552. ctx->ctxvals_pos += (0x0300/4 + (vs_nr * vs_len));
  553. if (ctx->mode != NVKM_GRCTX_VALS)
  554. return;
  555. offset += 0x0280/4;
  556. for (i = 0; i < 16; i++, offset += 2)
  557. nvkm_wo32(obj, offset * 4, 0x3f800000);
  558. for (vs = 0; vs < vs_nr; vs++, offset += vs_len) {
  559. for (i = 0; i < vs_nr_b0 * 6; i += 6)
  560. nvkm_wo32(obj, (offset + b0_offset + i) * 4, 0x00000001);
  561. for (i = 0; i < vs_nr_b1 * 4; i += 4)
  562. nvkm_wo32(obj, (offset + b1_offset + i) * 4, 0x3f800000);
  563. }
  564. }
  565. static void
  566. nv40_grctx_generate(struct nvkm_grctx *ctx)
  567. {
  568. /* decide whether we're loading/unloading the context */
  569. cp_bra (ctx, AUTO_SAVE, PENDING, cp_setup_save);
  570. cp_bra (ctx, USER_SAVE, PENDING, cp_setup_save);
  571. cp_name(ctx, cp_check_load);
  572. cp_bra (ctx, AUTO_LOAD, PENDING, cp_setup_auto_load);
  573. cp_bra (ctx, USER_LOAD, PENDING, cp_setup_load);
  574. cp_bra (ctx, ALWAYS, TRUE, cp_exit);
  575. /* setup for context load */
  576. cp_name(ctx, cp_setup_auto_load);
  577. cp_wait(ctx, STATUS, IDLE);
  578. cp_out (ctx, CP_NEXT_TO_SWAP);
  579. cp_name(ctx, cp_setup_load);
  580. cp_wait(ctx, STATUS, IDLE);
  581. cp_set (ctx, SWAP_DIRECTION, LOAD);
  582. cp_out (ctx, 0x00910880); /* ?? */
  583. cp_out (ctx, 0x00901ffe); /* ?? */
  584. cp_out (ctx, 0x01940000); /* ?? */
  585. cp_lsr (ctx, 0x20);
  586. cp_out (ctx, 0x0060000b); /* ?? */
  587. cp_wait(ctx, UNK57, CLEAR);
  588. cp_out (ctx, 0x0060000c); /* ?? */
  589. cp_bra (ctx, ALWAYS, TRUE, cp_swap_state);
  590. /* setup for context save */
  591. cp_name(ctx, cp_setup_save);
  592. cp_set (ctx, SWAP_DIRECTION, SAVE);
  593. /* general PGRAPH state */
  594. cp_name(ctx, cp_swap_state);
  595. cp_pos (ctx, 0x00020/4);
  596. nv40_gr_construct_general(ctx);
  597. cp_wait(ctx, STATUS, IDLE);
  598. /* 3D state, block 1 */
  599. cp_bra (ctx, UNK54, CLEAR, cp_prepare_exit);
  600. nv40_gr_construct_state3d(ctx);
  601. cp_wait(ctx, STATUS, IDLE);
  602. /* 3D state, block 2 */
  603. nv40_gr_construct_state3d_2(ctx);
  604. /* Some other block of "random" state */
  605. nv40_gr_construct_state3d_3(ctx);
  606. /* Per-vertex shader state */
  607. cp_pos (ctx, ctx->ctxvals_pos);
  608. nv40_gr_construct_shader(ctx);
  609. /* pre-exit state updates */
  610. cp_name(ctx, cp_prepare_exit);
  611. cp_bra (ctx, SWAP_DIRECTION, SAVE, cp_check_load);
  612. cp_bra (ctx, USER_SAVE, PENDING, cp_exit);
  613. cp_out (ctx, CP_NEXT_TO_CURRENT);
  614. cp_name(ctx, cp_exit);
  615. cp_set (ctx, USER_SAVE, NOT_PENDING);
  616. cp_set (ctx, USER_LOAD, NOT_PENDING);
  617. cp_out (ctx, CP_END);
  618. }
  619. void
  620. nv40_grctx_fill(struct nvkm_device *device, struct nvkm_gpuobj *mem)
  621. {
  622. nv40_grctx_generate(&(struct nvkm_grctx) {
  623. .device = device,
  624. .mode = NVKM_GRCTX_VALS,
  625. .data = mem,
  626. });
  627. }
  628. int
  629. nv40_grctx_init(struct nvkm_device *device, u32 *size)
  630. {
  631. u32 *ctxprog = kmalloc(256 * 4, GFP_KERNEL), i;
  632. struct nvkm_grctx ctx = {
  633. .device = device,
  634. .mode = NVKM_GRCTX_PROG,
  635. .ucode = ctxprog,
  636. .ctxprog_max = 256,
  637. };
  638. if (!ctxprog)
  639. return -ENOMEM;
  640. nv40_grctx_generate(&ctx);
  641. nvkm_wr32(device, 0x400324, 0);
  642. for (i = 0; i < ctx.ctxprog_len; i++)
  643. nvkm_wr32(device, 0x400328, ctxprog[i]);
  644. *size = ctx.ctxvals_pos * 4;
  645. kfree(ctxprog);
  646. return 0;
  647. }