nvc0_graph.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * Copyright 2010 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. #include <linux/firmware.h>
  25. #include "drmP.h"
  26. #include "nouveau_drv.h"
  27. #include "nouveau_mm.h"
  28. #include "nvc0_graph.h"
  29. #include "nvc0_grhub.fuc.h"
  30. #include "nvc0_grgpc.fuc.h"
  31. static void
  32. nvc0_graph_ctxctl_debug_unit(struct drm_device *dev, u32 base)
  33. {
  34. NV_INFO(dev, "PGRAPH: %06x - done 0x%08x\n", base,
  35. nv_rd32(dev, base + 0x400));
  36. NV_INFO(dev, "PGRAPH: %06x - stat 0x%08x 0x%08x 0x%08x 0x%08x\n", base,
  37. nv_rd32(dev, base + 0x800), nv_rd32(dev, base + 0x804),
  38. nv_rd32(dev, base + 0x808), nv_rd32(dev, base + 0x80c));
  39. NV_INFO(dev, "PGRAPH: %06x - stat 0x%08x 0x%08x 0x%08x 0x%08x\n", base,
  40. nv_rd32(dev, base + 0x810), nv_rd32(dev, base + 0x814),
  41. nv_rd32(dev, base + 0x818), nv_rd32(dev, base + 0x81c));
  42. }
  43. static void
  44. nvc0_graph_ctxctl_debug(struct drm_device *dev)
  45. {
  46. u32 gpcnr = nv_rd32(dev, 0x409604) & 0xffff;
  47. u32 gpc;
  48. nvc0_graph_ctxctl_debug_unit(dev, 0x409000);
  49. for (gpc = 0; gpc < gpcnr; gpc++)
  50. nvc0_graph_ctxctl_debug_unit(dev, 0x502000 + (gpc * 0x8000));
  51. }
  52. static int
  53. nvc0_graph_load_context(struct nouveau_channel *chan)
  54. {
  55. struct drm_device *dev = chan->dev;
  56. nv_wr32(dev, 0x409840, 0x00000030);
  57. nv_wr32(dev, 0x409500, 0x80000000 | chan->ramin->vinst >> 12);
  58. nv_wr32(dev, 0x409504, 0x00000003);
  59. if (!nv_wait(dev, 0x409800, 0x00000010, 0x00000010))
  60. NV_ERROR(dev, "PGRAPH: load_ctx timeout\n");
  61. return 0;
  62. }
  63. static int
  64. nvc0_graph_unload_context_to(struct drm_device *dev, u64 chan)
  65. {
  66. nv_wr32(dev, 0x409840, 0x00000003);
  67. nv_wr32(dev, 0x409500, 0x80000000 | chan >> 12);
  68. nv_wr32(dev, 0x409504, 0x00000009);
  69. if (!nv_wait(dev, 0x409800, 0x00000001, 0x00000000)) {
  70. NV_ERROR(dev, "PGRAPH: unload_ctx timeout\n");
  71. return -EBUSY;
  72. }
  73. return 0;
  74. }
  75. static int
  76. nvc0_graph_construct_context(struct nouveau_channel *chan)
  77. {
  78. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  79. struct nvc0_graph_priv *priv = nv_engine(chan->dev, NVOBJ_ENGINE_GR);
  80. struct nvc0_graph_chan *grch = chan->engctx[NVOBJ_ENGINE_GR];
  81. struct drm_device *dev = chan->dev;
  82. int ret, i;
  83. u32 *ctx;
  84. ctx = kmalloc(priv->grctx_size, GFP_KERNEL);
  85. if (!ctx)
  86. return -ENOMEM;
  87. if (!nouveau_ctxfw) {
  88. nv_wr32(dev, 0x409840, 0x80000000);
  89. nv_wr32(dev, 0x409500, 0x80000000 | chan->ramin->vinst >> 12);
  90. nv_wr32(dev, 0x409504, 0x00000001);
  91. if (!nv_wait(dev, 0x409800, 0x80000000, 0x80000000)) {
  92. NV_ERROR(dev, "PGRAPH: HUB_SET_CHAN timeout\n");
  93. nvc0_graph_ctxctl_debug(dev);
  94. return -EBUSY;
  95. }
  96. } else {
  97. nvc0_graph_load_context(chan);
  98. nv_wo32(grch->grctx, 0x1c, 1);
  99. nv_wo32(grch->grctx, 0x20, 0);
  100. nv_wo32(grch->grctx, 0x28, 0);
  101. nv_wo32(grch->grctx, 0x2c, 0);
  102. dev_priv->engine.instmem.flush(dev);
  103. }
  104. ret = nvc0_grctx_generate(chan);
  105. if (ret) {
  106. kfree(ctx);
  107. return ret;
  108. }
  109. if (!nouveau_ctxfw) {
  110. nv_wr32(dev, 0x409840, 0x80000000);
  111. nv_wr32(dev, 0x409500, 0x80000000 | chan->ramin->vinst >> 12);
  112. nv_wr32(dev, 0x409504, 0x00000002);
  113. if (!nv_wait(dev, 0x409800, 0x80000000, 0x80000000)) {
  114. NV_ERROR(dev, "PGRAPH: HUB_CTX_SAVE timeout\n");
  115. nvc0_graph_ctxctl_debug(dev);
  116. return -EBUSY;
  117. }
  118. } else {
  119. ret = nvc0_graph_unload_context_to(dev, chan->ramin->vinst);
  120. if (ret) {
  121. kfree(ctx);
  122. return ret;
  123. }
  124. }
  125. for (i = 0; i < priv->grctx_size; i += 4)
  126. ctx[i / 4] = nv_ro32(grch->grctx, i);
  127. priv->grctx_vals = ctx;
  128. return 0;
  129. }
  130. static int
  131. nvc0_graph_create_context_mmio_list(struct nouveau_channel *chan)
  132. {
  133. struct nvc0_graph_priv *priv = nv_engine(chan->dev, NVOBJ_ENGINE_GR);
  134. struct nvc0_graph_chan *grch = chan->engctx[NVOBJ_ENGINE_GR];
  135. struct drm_device *dev = chan->dev;
  136. int i = 0, gpc, tp, ret;
  137. u32 magic;
  138. ret = nouveau_gpuobj_new(dev, NULL, 0x2000, 256, NVOBJ_FLAG_VM,
  139. &grch->unk408004);
  140. if (ret)
  141. return ret;
  142. ret = nouveau_gpuobj_new(dev, NULL, 0x8000, 256, NVOBJ_FLAG_VM,
  143. &grch->unk40800c);
  144. if (ret)
  145. return ret;
  146. ret = nouveau_gpuobj_new(dev, NULL, 384 * 1024, 4096,
  147. NVOBJ_FLAG_VM | NVOBJ_FLAG_VM_USER,
  148. &grch->unk418810);
  149. if (ret)
  150. return ret;
  151. ret = nouveau_gpuobj_new(dev, NULL, 0x1000, 0, NVOBJ_FLAG_VM,
  152. &grch->mmio);
  153. if (ret)
  154. return ret;
  155. nv_wo32(grch->mmio, i++ * 4, 0x00408004);
  156. nv_wo32(grch->mmio, i++ * 4, grch->unk408004->linst >> 8);
  157. nv_wo32(grch->mmio, i++ * 4, 0x00408008);
  158. nv_wo32(grch->mmio, i++ * 4, 0x80000018);
  159. nv_wo32(grch->mmio, i++ * 4, 0x0040800c);
  160. nv_wo32(grch->mmio, i++ * 4, grch->unk40800c->linst >> 8);
  161. nv_wo32(grch->mmio, i++ * 4, 0x00408010);
  162. nv_wo32(grch->mmio, i++ * 4, 0x80000000);
  163. nv_wo32(grch->mmio, i++ * 4, 0x00418810);
  164. nv_wo32(grch->mmio, i++ * 4, 0x80000000 | grch->unk418810->linst >> 12);
  165. nv_wo32(grch->mmio, i++ * 4, 0x00419848);
  166. nv_wo32(grch->mmio, i++ * 4, 0x10000000 | grch->unk418810->linst >> 12);
  167. nv_wo32(grch->mmio, i++ * 4, 0x00419004);
  168. nv_wo32(grch->mmio, i++ * 4, grch->unk40800c->linst >> 8);
  169. nv_wo32(grch->mmio, i++ * 4, 0x00419008);
  170. nv_wo32(grch->mmio, i++ * 4, 0x00000000);
  171. nv_wo32(grch->mmio, i++ * 4, 0x00418808);
  172. nv_wo32(grch->mmio, i++ * 4, grch->unk408004->linst >> 8);
  173. nv_wo32(grch->mmio, i++ * 4, 0x0041880c);
  174. nv_wo32(grch->mmio, i++ * 4, 0x80000018);
  175. magic = 0x02180000;
  176. nv_wo32(grch->mmio, i++ * 4, 0x00405830);
  177. nv_wo32(grch->mmio, i++ * 4, magic);
  178. for (gpc = 0; gpc < priv->gpc_nr; gpc++) {
  179. for (tp = 0; tp < priv->tp_nr[gpc]; tp++, magic += 0x0324) {
  180. u32 reg = 0x504520 + (gpc * 0x8000) + (tp * 0x0800);
  181. nv_wo32(grch->mmio, i++ * 4, reg);
  182. nv_wo32(grch->mmio, i++ * 4, magic);
  183. }
  184. }
  185. grch->mmio_nr = i / 2;
  186. return 0;
  187. }
  188. static int
  189. nvc0_graph_context_new(struct nouveau_channel *chan, int engine)
  190. {
  191. struct drm_device *dev = chan->dev;
  192. struct drm_nouveau_private *dev_priv = dev->dev_private;
  193. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  194. struct nvc0_graph_priv *priv = nv_engine(dev, engine);
  195. struct nvc0_graph_chan *grch;
  196. struct nouveau_gpuobj *grctx;
  197. int ret, i;
  198. grch = kzalloc(sizeof(*grch), GFP_KERNEL);
  199. if (!grch)
  200. return -ENOMEM;
  201. chan->engctx[NVOBJ_ENGINE_GR] = grch;
  202. ret = nouveau_gpuobj_new(dev, NULL, priv->grctx_size, 256,
  203. NVOBJ_FLAG_VM | NVOBJ_FLAG_ZERO_ALLOC,
  204. &grch->grctx);
  205. if (ret)
  206. goto error;
  207. grctx = grch->grctx;
  208. ret = nvc0_graph_create_context_mmio_list(chan);
  209. if (ret)
  210. goto error;
  211. nv_wo32(chan->ramin, 0x0210, lower_32_bits(grctx->linst) | 4);
  212. nv_wo32(chan->ramin, 0x0214, upper_32_bits(grctx->linst));
  213. pinstmem->flush(dev);
  214. if (!priv->grctx_vals) {
  215. ret = nvc0_graph_construct_context(chan);
  216. if (ret)
  217. goto error;
  218. }
  219. for (i = 0; i < priv->grctx_size; i += 4)
  220. nv_wo32(grctx, i, priv->grctx_vals[i / 4]);
  221. if (!nouveau_ctxfw) {
  222. nv_wo32(grctx, 0x00, grch->mmio_nr);
  223. nv_wo32(grctx, 0x04, grch->mmio->linst >> 8);
  224. } else {
  225. nv_wo32(grctx, 0xf4, 0);
  226. nv_wo32(grctx, 0xf8, 0);
  227. nv_wo32(grctx, 0x10, grch->mmio_nr);
  228. nv_wo32(grctx, 0x14, lower_32_bits(grch->mmio->linst));
  229. nv_wo32(grctx, 0x18, upper_32_bits(grch->mmio->linst));
  230. nv_wo32(grctx, 0x1c, 1);
  231. nv_wo32(grctx, 0x20, 0);
  232. nv_wo32(grctx, 0x28, 0);
  233. nv_wo32(grctx, 0x2c, 0);
  234. }
  235. pinstmem->flush(dev);
  236. return 0;
  237. error:
  238. priv->base.context_del(chan, engine);
  239. return ret;
  240. }
  241. static void
  242. nvc0_graph_context_del(struct nouveau_channel *chan, int engine)
  243. {
  244. struct nvc0_graph_chan *grch = chan->engctx[engine];
  245. nouveau_gpuobj_ref(NULL, &grch->mmio);
  246. nouveau_gpuobj_ref(NULL, &grch->unk418810);
  247. nouveau_gpuobj_ref(NULL, &grch->unk40800c);
  248. nouveau_gpuobj_ref(NULL, &grch->unk408004);
  249. nouveau_gpuobj_ref(NULL, &grch->grctx);
  250. chan->engctx[engine] = NULL;
  251. }
  252. static int
  253. nvc0_graph_object_new(struct nouveau_channel *chan, int engine,
  254. u32 handle, u16 class)
  255. {
  256. return 0;
  257. }
  258. static int
  259. nvc0_graph_fini(struct drm_device *dev, int engine)
  260. {
  261. return 0;
  262. }
  263. static int
  264. nvc0_graph_mthd_page_flip(struct nouveau_channel *chan,
  265. u32 class, u32 mthd, u32 data)
  266. {
  267. nouveau_finish_page_flip(chan, NULL);
  268. return 0;
  269. }
  270. static void
  271. nvc0_graph_init_obj418880(struct drm_device *dev)
  272. {
  273. struct nvc0_graph_priv *priv = nv_engine(dev, NVOBJ_ENGINE_GR);
  274. int i;
  275. nv_wr32(dev, GPC_BCAST(0x0880), 0x00000000);
  276. nv_wr32(dev, GPC_BCAST(0x08a4), 0x00000000);
  277. for (i = 0; i < 4; i++)
  278. nv_wr32(dev, GPC_BCAST(0x0888) + (i * 4), 0x00000000);
  279. nv_wr32(dev, GPC_BCAST(0x08b4), priv->unk4188b4->vinst >> 8);
  280. nv_wr32(dev, GPC_BCAST(0x08b8), priv->unk4188b8->vinst >> 8);
  281. }
  282. static void
  283. nvc0_graph_init_regs(struct drm_device *dev)
  284. {
  285. nv_wr32(dev, 0x400080, 0x003083c2);
  286. nv_wr32(dev, 0x400088, 0x00006fe7);
  287. nv_wr32(dev, 0x40008c, 0x00000000);
  288. nv_wr32(dev, 0x400090, 0x00000030);
  289. nv_wr32(dev, 0x40013c, 0x013901f7);
  290. nv_wr32(dev, 0x400140, 0x00000100);
  291. nv_wr32(dev, 0x400144, 0x00000000);
  292. nv_wr32(dev, 0x400148, 0x00000110);
  293. nv_wr32(dev, 0x400138, 0x00000000);
  294. nv_wr32(dev, 0x400130, 0x00000000);
  295. nv_wr32(dev, 0x400134, 0x00000000);
  296. nv_wr32(dev, 0x400124, 0x00000002);
  297. }
  298. static void
  299. nvc0_graph_init_gpc_0(struct drm_device *dev)
  300. {
  301. struct nvc0_graph_priv *priv = nv_engine(dev, NVOBJ_ENGINE_GR);
  302. u32 data[TP_MAX / 8];
  303. u8 tpnr[GPC_MAX];
  304. int i, gpc, tpc;
  305. /*
  306. * TP ROP UNKVAL(magic_not_rop_nr)
  307. * 450: 4/0/0/0 2 3
  308. * 460: 3/4/0/0 4 1
  309. * 465: 3/4/4/0 4 7
  310. * 470: 3/3/4/4 5 5
  311. * 480: 3/4/4/4 6 6
  312. *
  313. * magicgpc918
  314. * 450: 00200000 00000000001000000000000000000000
  315. * 460: 00124925 00000000000100100100100100100101
  316. * 465: 000ba2e9 00000000000010111010001011101001
  317. * 470: 00092493 00000000000010010010010010010011
  318. * 480: 00088889 00000000000010001000100010001001
  319. */
  320. memset(data, 0x00, sizeof(data));
  321. memcpy(tpnr, priv->tp_nr, sizeof(priv->tp_nr));
  322. for (i = 0, gpc = -1; i < priv->tp_total; i++) {
  323. do {
  324. gpc = (gpc + 1) % priv->gpc_nr;
  325. } while (!tpnr[gpc]);
  326. tpc = priv->tp_nr[gpc] - tpnr[gpc]--;
  327. data[i / 8] |= tpc << ((i % 8) * 4);
  328. }
  329. nv_wr32(dev, GPC_BCAST(0x0980), data[0]);
  330. nv_wr32(dev, GPC_BCAST(0x0984), data[1]);
  331. nv_wr32(dev, GPC_BCAST(0x0988), data[2]);
  332. nv_wr32(dev, GPC_BCAST(0x098c), data[3]);
  333. for (gpc = 0; gpc < priv->gpc_nr; gpc++) {
  334. nv_wr32(dev, GPC_UNIT(gpc, 0x0914), priv->magic_not_rop_nr << 8 |
  335. priv->tp_nr[gpc]);
  336. nv_wr32(dev, GPC_UNIT(gpc, 0x0910), 0x00040000 | priv->tp_total);
  337. nv_wr32(dev, GPC_UNIT(gpc, 0x0918), priv->magicgpc918);
  338. }
  339. nv_wr32(dev, GPC_BCAST(0x1bd4), priv->magicgpc918);
  340. nv_wr32(dev, GPC_BCAST(0x08ac), priv->rop_nr);
  341. }
  342. static void
  343. nvc0_graph_init_units(struct drm_device *dev)
  344. {
  345. nv_wr32(dev, 0x409c24, 0x000f0000);
  346. nv_wr32(dev, 0x404000, 0xc0000000); /* DISPATCH */
  347. nv_wr32(dev, 0x404600, 0xc0000000); /* M2MF */
  348. nv_wr32(dev, 0x408030, 0xc0000000);
  349. nv_wr32(dev, 0x40601c, 0xc0000000);
  350. nv_wr32(dev, 0x404490, 0xc0000000); /* MACRO */
  351. nv_wr32(dev, 0x406018, 0xc0000000);
  352. nv_wr32(dev, 0x405840, 0xc0000000);
  353. nv_wr32(dev, 0x405844, 0x00ffffff);
  354. nv_mask(dev, 0x419cc0, 0x00000008, 0x00000008);
  355. nv_mask(dev, 0x419eb4, 0x00001000, 0x00001000);
  356. }
  357. static void
  358. nvc0_graph_init_gpc_1(struct drm_device *dev)
  359. {
  360. struct nvc0_graph_priv *priv = nv_engine(dev, NVOBJ_ENGINE_GR);
  361. int gpc, tp;
  362. for (gpc = 0; gpc < priv->gpc_nr; gpc++) {
  363. nv_wr32(dev, GPC_UNIT(gpc, 0x0420), 0xc0000000);
  364. nv_wr32(dev, GPC_UNIT(gpc, 0x0900), 0xc0000000);
  365. nv_wr32(dev, GPC_UNIT(gpc, 0x1028), 0xc0000000);
  366. nv_wr32(dev, GPC_UNIT(gpc, 0x0824), 0xc0000000);
  367. for (tp = 0; tp < priv->tp_nr[gpc]; tp++) {
  368. nv_wr32(dev, TP_UNIT(gpc, tp, 0x508), 0xffffffff);
  369. nv_wr32(dev, TP_UNIT(gpc, tp, 0x50c), 0xffffffff);
  370. nv_wr32(dev, TP_UNIT(gpc, tp, 0x224), 0xc0000000);
  371. nv_wr32(dev, TP_UNIT(gpc, tp, 0x48c), 0xc0000000);
  372. nv_wr32(dev, TP_UNIT(gpc, tp, 0x084), 0xc0000000);
  373. nv_wr32(dev, TP_UNIT(gpc, tp, 0x644), 0x001ffffe);
  374. nv_wr32(dev, TP_UNIT(gpc, tp, 0x64c), 0x0000000f);
  375. }
  376. nv_wr32(dev, GPC_UNIT(gpc, 0x2c90), 0xffffffff);
  377. nv_wr32(dev, GPC_UNIT(gpc, 0x2c94), 0xffffffff);
  378. }
  379. }
  380. static void
  381. nvc0_graph_init_rop(struct drm_device *dev)
  382. {
  383. struct nvc0_graph_priv *priv = nv_engine(dev, NVOBJ_ENGINE_GR);
  384. int rop;
  385. for (rop = 0; rop < priv->rop_nr; rop++) {
  386. nv_wr32(dev, ROP_UNIT(rop, 0x144), 0xc0000000);
  387. nv_wr32(dev, ROP_UNIT(rop, 0x070), 0xc0000000);
  388. nv_wr32(dev, ROP_UNIT(rop, 0x204), 0xffffffff);
  389. nv_wr32(dev, ROP_UNIT(rop, 0x208), 0xffffffff);
  390. }
  391. }
  392. static void
  393. nvc0_graph_init_fuc(struct drm_device *dev, u32 fuc_base,
  394. struct nvc0_graph_fuc *code, struct nvc0_graph_fuc *data)
  395. {
  396. int i;
  397. nv_wr32(dev, fuc_base + 0x01c0, 0x01000000);
  398. for (i = 0; i < data->size / 4; i++)
  399. nv_wr32(dev, fuc_base + 0x01c4, data->data[i]);
  400. nv_wr32(dev, fuc_base + 0x0180, 0x01000000);
  401. for (i = 0; i < code->size / 4; i++) {
  402. if ((i & 0x3f) == 0)
  403. nv_wr32(dev, fuc_base + 0x0188, i >> 6);
  404. nv_wr32(dev, fuc_base + 0x0184, code->data[i]);
  405. }
  406. }
  407. static int
  408. nvc0_graph_init_ctxctl(struct drm_device *dev)
  409. {
  410. struct drm_nouveau_private *dev_priv = dev->dev_private;
  411. struct nvc0_graph_priv *priv = nv_engine(dev, NVOBJ_ENGINE_GR);
  412. u32 r000260;
  413. int i;
  414. if (!nouveau_ctxfw) {
  415. /* load HUB microcode */
  416. r000260 = nv_mask(dev, 0x000260, 0x00000001, 0x00000000);
  417. nv_wr32(dev, 0x4091c0, 0x01000000);
  418. for (i = 0; i < sizeof(nvc0_grhub_data) / 4; i++)
  419. nv_wr32(dev, 0x4091c4, nvc0_grhub_data[i]);
  420. nv_wr32(dev, 0x409180, 0x01000000);
  421. for (i = 0; i < sizeof(nvc0_grhub_code) / 4; i++) {
  422. if ((i & 0x3f) == 0)
  423. nv_wr32(dev, 0x409188, i >> 6);
  424. nv_wr32(dev, 0x409184, nvc0_grhub_code[i]);
  425. }
  426. /* load GPC microcode */
  427. nv_wr32(dev, 0x41a1c0, 0x01000000);
  428. for (i = 0; i < sizeof(nvc0_grgpc_data) / 4; i++)
  429. nv_wr32(dev, 0x41a1c4, nvc0_grgpc_data[i]);
  430. nv_wr32(dev, 0x41a180, 0x01000000);
  431. for (i = 0; i < sizeof(nvc0_grgpc_code) / 4; i++) {
  432. if ((i & 0x3f) == 0)
  433. nv_wr32(dev, 0x41a188, i >> 6);
  434. nv_wr32(dev, 0x41a184, nvc0_grgpc_code[i]);
  435. }
  436. nv_wr32(dev, 0x000260, r000260);
  437. /* start HUB ucode running, it'll init the GPCs */
  438. nv_wr32(dev, 0x409800, dev_priv->chipset);
  439. nv_wr32(dev, 0x40910c, 0x00000000);
  440. nv_wr32(dev, 0x409100, 0x00000002);
  441. if (!nv_wait(dev, 0x409800, 0x80000000, 0x80000000)) {
  442. NV_ERROR(dev, "PGRAPH: HUB_INIT timed out\n");
  443. nvc0_graph_ctxctl_debug(dev);
  444. return -EBUSY;
  445. }
  446. priv->grctx_size = nv_rd32(dev, 0x409804);
  447. return 0;
  448. }
  449. /* load fuc microcode */
  450. r000260 = nv_mask(dev, 0x000260, 0x00000001, 0x00000000);
  451. nvc0_graph_init_fuc(dev, 0x409000, &priv->fuc409c, &priv->fuc409d);
  452. nvc0_graph_init_fuc(dev, 0x41a000, &priv->fuc41ac, &priv->fuc41ad);
  453. nv_wr32(dev, 0x000260, r000260);
  454. /* start both of them running */
  455. nv_wr32(dev, 0x409840, 0xffffffff);
  456. nv_wr32(dev, 0x41a10c, 0x00000000);
  457. nv_wr32(dev, 0x40910c, 0x00000000);
  458. nv_wr32(dev, 0x41a100, 0x00000002);
  459. nv_wr32(dev, 0x409100, 0x00000002);
  460. if (!nv_wait(dev, 0x409800, 0x00000001, 0x00000001))
  461. NV_INFO(dev, "0x409800 wait failed\n");
  462. nv_wr32(dev, 0x409840, 0xffffffff);
  463. nv_wr32(dev, 0x409500, 0x7fffffff);
  464. nv_wr32(dev, 0x409504, 0x00000021);
  465. nv_wr32(dev, 0x409840, 0xffffffff);
  466. nv_wr32(dev, 0x409500, 0x00000000);
  467. nv_wr32(dev, 0x409504, 0x00000010);
  468. if (!nv_wait_ne(dev, 0x409800, 0xffffffff, 0x00000000)) {
  469. NV_ERROR(dev, "fuc09 req 0x10 timeout\n");
  470. return -EBUSY;
  471. }
  472. priv->grctx_size = nv_rd32(dev, 0x409800);
  473. nv_wr32(dev, 0x409840, 0xffffffff);
  474. nv_wr32(dev, 0x409500, 0x00000000);
  475. nv_wr32(dev, 0x409504, 0x00000016);
  476. if (!nv_wait_ne(dev, 0x409800, 0xffffffff, 0x00000000)) {
  477. NV_ERROR(dev, "fuc09 req 0x16 timeout\n");
  478. return -EBUSY;
  479. }
  480. nv_wr32(dev, 0x409840, 0xffffffff);
  481. nv_wr32(dev, 0x409500, 0x00000000);
  482. nv_wr32(dev, 0x409504, 0x00000025);
  483. if (!nv_wait_ne(dev, 0x409800, 0xffffffff, 0x00000000)) {
  484. NV_ERROR(dev, "fuc09 req 0x25 timeout\n");
  485. return -EBUSY;
  486. }
  487. return 0;
  488. }
  489. static int
  490. nvc0_graph_init(struct drm_device *dev, int engine)
  491. {
  492. int ret;
  493. nv_mask(dev, 0x000200, 0x18001000, 0x00000000);
  494. nv_mask(dev, 0x000200, 0x18001000, 0x18001000);
  495. nvc0_graph_init_obj418880(dev);
  496. nvc0_graph_init_regs(dev);
  497. /*nvc0_graph_init_unitplemented_magics(dev);*/
  498. nvc0_graph_init_gpc_0(dev);
  499. /*nvc0_graph_init_unitplemented_c242(dev);*/
  500. nv_wr32(dev, 0x400500, 0x00010001);
  501. nv_wr32(dev, 0x400100, 0xffffffff);
  502. nv_wr32(dev, 0x40013c, 0xffffffff);
  503. nvc0_graph_init_units(dev);
  504. nvc0_graph_init_gpc_1(dev);
  505. nvc0_graph_init_rop(dev);
  506. nv_wr32(dev, 0x400108, 0xffffffff);
  507. nv_wr32(dev, 0x400138, 0xffffffff);
  508. nv_wr32(dev, 0x400118, 0xffffffff);
  509. nv_wr32(dev, 0x400130, 0xffffffff);
  510. nv_wr32(dev, 0x40011c, 0xffffffff);
  511. nv_wr32(dev, 0x400134, 0xffffffff);
  512. nv_wr32(dev, 0x400054, 0x34ce3464);
  513. ret = nvc0_graph_init_ctxctl(dev);
  514. if (ret)
  515. return ret;
  516. return 0;
  517. }
  518. int
  519. nvc0_graph_isr_chid(struct drm_device *dev, u64 inst)
  520. {
  521. struct drm_nouveau_private *dev_priv = dev->dev_private;
  522. struct nouveau_channel *chan;
  523. unsigned long flags;
  524. int i;
  525. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  526. for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
  527. chan = dev_priv->channels.ptr[i];
  528. if (!chan || !chan->ramin)
  529. continue;
  530. if (inst == chan->ramin->vinst)
  531. break;
  532. }
  533. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  534. return i;
  535. }
  536. static void
  537. nvc0_graph_ctxctl_isr(struct drm_device *dev)
  538. {
  539. u32 ustat = nv_rd32(dev, 0x409c18);
  540. if (ustat & 0x00000001)
  541. NV_INFO(dev, "PGRAPH: CTXCTRL ucode error\n");
  542. if (ustat & 0x00080000)
  543. NV_INFO(dev, "PGRAPH: CTXCTRL watchdog timeout\n");
  544. if (ustat & ~0x00080001)
  545. NV_INFO(dev, "PGRAPH: CTXCTRL 0x%08x\n", ustat);
  546. nvc0_graph_ctxctl_debug(dev);
  547. nv_wr32(dev, 0x409c20, ustat);
  548. }
  549. static void
  550. nvc0_graph_isr(struct drm_device *dev)
  551. {
  552. u64 inst = (u64)(nv_rd32(dev, 0x409b00) & 0x0fffffff) << 12;
  553. u32 chid = nvc0_graph_isr_chid(dev, inst);
  554. u32 stat = nv_rd32(dev, 0x400100);
  555. u32 addr = nv_rd32(dev, 0x400704);
  556. u32 mthd = (addr & 0x00003ffc);
  557. u32 subc = (addr & 0x00070000) >> 16;
  558. u32 data = nv_rd32(dev, 0x400708);
  559. u32 code = nv_rd32(dev, 0x400110);
  560. u32 class = nv_rd32(dev, 0x404200 + (subc * 4));
  561. if (stat & 0x00000010) {
  562. if (nouveau_gpuobj_mthd_call2(dev, chid, class, mthd, data)) {
  563. NV_INFO(dev, "PGRAPH: ILLEGAL_MTHD ch %d [0x%010llx] "
  564. "subc %d class 0x%04x mthd 0x%04x "
  565. "data 0x%08x\n",
  566. chid, inst, subc, class, mthd, data);
  567. }
  568. nv_wr32(dev, 0x400100, 0x00000010);
  569. stat &= ~0x00000010;
  570. }
  571. if (stat & 0x00000020) {
  572. NV_INFO(dev, "PGRAPH: ILLEGAL_CLASS ch %d [0x%010llx] subc %d "
  573. "class 0x%04x mthd 0x%04x data 0x%08x\n",
  574. chid, inst, subc, class, mthd, data);
  575. nv_wr32(dev, 0x400100, 0x00000020);
  576. stat &= ~0x00000020;
  577. }
  578. if (stat & 0x00100000) {
  579. NV_INFO(dev, "PGRAPH: DATA_ERROR [");
  580. nouveau_enum_print(nv50_data_error_names, code);
  581. printk("] ch %d [0x%010llx] subc %d class 0x%04x "
  582. "mthd 0x%04x data 0x%08x\n",
  583. chid, inst, subc, class, mthd, data);
  584. nv_wr32(dev, 0x400100, 0x00100000);
  585. stat &= ~0x00100000;
  586. }
  587. if (stat & 0x00200000) {
  588. u32 trap = nv_rd32(dev, 0x400108);
  589. NV_INFO(dev, "PGRAPH: TRAP ch %d status 0x%08x\n", chid, trap);
  590. nv_wr32(dev, 0x400108, trap);
  591. nv_wr32(dev, 0x400100, 0x00200000);
  592. stat &= ~0x00200000;
  593. }
  594. if (stat & 0x00080000) {
  595. nvc0_graph_ctxctl_isr(dev);
  596. nv_wr32(dev, 0x400100, 0x00080000);
  597. stat &= ~0x00080000;
  598. }
  599. if (stat) {
  600. NV_INFO(dev, "PGRAPH: unknown stat 0x%08x\n", stat);
  601. nv_wr32(dev, 0x400100, stat);
  602. }
  603. nv_wr32(dev, 0x400500, 0x00010001);
  604. }
  605. static void
  606. nvc0_runk140_isr(struct drm_device *dev)
  607. {
  608. u32 units = nv_rd32(dev, 0x00017c) & 0x1f;
  609. while (units) {
  610. u32 unit = ffs(units) - 1;
  611. u32 reg = 0x140000 + unit * 0x2000;
  612. u32 st0 = nv_mask(dev, reg + 0x1020, 0, 0);
  613. u32 st1 = nv_mask(dev, reg + 0x1420, 0, 0);
  614. NV_INFO(dev, "PRUNK140: %d 0x%08x 0x%08x\n", unit, st0, st1);
  615. units &= ~(1 << unit);
  616. }
  617. }
  618. static int
  619. nvc0_graph_create_fw(struct drm_device *dev, const char *fwname,
  620. struct nvc0_graph_fuc *fuc)
  621. {
  622. struct drm_nouveau_private *dev_priv = dev->dev_private;
  623. const struct firmware *fw;
  624. char f[32];
  625. int ret;
  626. snprintf(f, sizeof(f), "nouveau/nv%02x_%s", dev_priv->chipset, fwname);
  627. ret = request_firmware(&fw, f, &dev->pdev->dev);
  628. if (ret) {
  629. snprintf(f, sizeof(f), "nouveau/%s", fwname);
  630. ret = request_firmware(&fw, f, &dev->pdev->dev);
  631. if (ret) {
  632. NV_ERROR(dev, "failed to load %s\n", fwname);
  633. return ret;
  634. }
  635. }
  636. fuc->size = fw->size;
  637. fuc->data = kmemdup(fw->data, fuc->size, GFP_KERNEL);
  638. release_firmware(fw);
  639. return (fuc->data != NULL) ? 0 : -ENOMEM;
  640. }
  641. static void
  642. nvc0_graph_destroy_fw(struct nvc0_graph_fuc *fuc)
  643. {
  644. if (fuc->data) {
  645. kfree(fuc->data);
  646. fuc->data = NULL;
  647. }
  648. }
  649. static void
  650. nvc0_graph_destroy(struct drm_device *dev, int engine)
  651. {
  652. struct nvc0_graph_priv *priv = nv_engine(dev, engine);
  653. if (nouveau_ctxfw) {
  654. nvc0_graph_destroy_fw(&priv->fuc409c);
  655. nvc0_graph_destroy_fw(&priv->fuc409d);
  656. nvc0_graph_destroy_fw(&priv->fuc41ac);
  657. nvc0_graph_destroy_fw(&priv->fuc41ad);
  658. }
  659. nouveau_irq_unregister(dev, 12);
  660. nouveau_irq_unregister(dev, 25);
  661. nouveau_gpuobj_ref(NULL, &priv->unk4188b8);
  662. nouveau_gpuobj_ref(NULL, &priv->unk4188b4);
  663. if (priv->grctx_vals)
  664. kfree(priv->grctx_vals);
  665. NVOBJ_ENGINE_DEL(dev, GR);
  666. kfree(priv);
  667. }
  668. int
  669. nvc0_graph_create(struct drm_device *dev)
  670. {
  671. struct drm_nouveau_private *dev_priv = dev->dev_private;
  672. struct nvc0_graph_priv *priv;
  673. int ret, gpc, i;
  674. u32 fermi;
  675. fermi = nvc0_graph_class(dev);
  676. if (!fermi) {
  677. NV_ERROR(dev, "PGRAPH: unsupported chipset, please report!\n");
  678. return 0;
  679. }
  680. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  681. if (!priv)
  682. return -ENOMEM;
  683. priv->base.destroy = nvc0_graph_destroy;
  684. priv->base.init = nvc0_graph_init;
  685. priv->base.fini = nvc0_graph_fini;
  686. priv->base.context_new = nvc0_graph_context_new;
  687. priv->base.context_del = nvc0_graph_context_del;
  688. priv->base.object_new = nvc0_graph_object_new;
  689. NVOBJ_ENGINE_ADD(dev, GR, &priv->base);
  690. nouveau_irq_register(dev, 12, nvc0_graph_isr);
  691. nouveau_irq_register(dev, 25, nvc0_runk140_isr);
  692. if (nouveau_ctxfw) {
  693. NV_INFO(dev, "PGRAPH: using external firmware\n");
  694. if (nvc0_graph_create_fw(dev, "fuc409c", &priv->fuc409c) ||
  695. nvc0_graph_create_fw(dev, "fuc409d", &priv->fuc409d) ||
  696. nvc0_graph_create_fw(dev, "fuc41ac", &priv->fuc41ac) ||
  697. nvc0_graph_create_fw(dev, "fuc41ad", &priv->fuc41ad)) {
  698. ret = 0;
  699. goto error;
  700. }
  701. }
  702. ret = nouveau_gpuobj_new(dev, NULL, 0x1000, 256, 0, &priv->unk4188b4);
  703. if (ret)
  704. goto error;
  705. ret = nouveau_gpuobj_new(dev, NULL, 0x1000, 256, 0, &priv->unk4188b8);
  706. if (ret)
  707. goto error;
  708. for (i = 0; i < 0x1000; i += 4) {
  709. nv_wo32(priv->unk4188b4, i, 0x00000010);
  710. nv_wo32(priv->unk4188b8, i, 0x00000010);
  711. }
  712. priv->gpc_nr = nv_rd32(dev, 0x409604) & 0x0000001f;
  713. priv->rop_nr = (nv_rd32(dev, 0x409604) & 0x001f0000) >> 16;
  714. for (gpc = 0; gpc < priv->gpc_nr; gpc++) {
  715. priv->tp_nr[gpc] = nv_rd32(dev, GPC_UNIT(gpc, 0x2608));
  716. priv->tp_total += priv->tp_nr[gpc];
  717. }
  718. /*XXX: these need figuring out... */
  719. switch (dev_priv->chipset) {
  720. case 0xc0:
  721. if (priv->tp_total == 11) { /* 465, 3/4/4/0, 4 */
  722. priv->magic_not_rop_nr = 0x07;
  723. /* filled values up to tp_total, the rest 0 */
  724. priv->magicgpc918 = 0x000ba2e9;
  725. } else
  726. if (priv->tp_total == 14) { /* 470, 3/3/4/4, 5 */
  727. priv->magic_not_rop_nr = 0x05;
  728. priv->magicgpc918 = 0x00092493;
  729. } else
  730. if (priv->tp_total == 15) { /* 480, 3/4/4/4, 6 */
  731. priv->magic_not_rop_nr = 0x06;
  732. priv->magicgpc918 = 0x00088889;
  733. }
  734. break;
  735. case 0xc3: /* 450, 4/0/0/0, 2 */
  736. priv->magic_not_rop_nr = 0x03;
  737. priv->magicgpc918 = 0x00200000;
  738. break;
  739. case 0xc4: /* 460, 3/4/0/0, 4 */
  740. priv->magic_not_rop_nr = 0x01;
  741. priv->magicgpc918 = 0x00124925;
  742. break;
  743. case 0xc1: /* 2/0/0/0, 1 */
  744. priv->magic_not_rop_nr = 0x01;
  745. priv->magicgpc918 = 0x00400000;
  746. break;
  747. case 0xc8: /* 4/4/3/4, 5 */
  748. priv->magic_not_rop_nr = 0x06;
  749. priv->magicgpc918 = 0x00088889;
  750. break;
  751. case 0xce: /* 4/4/0/0, 4 */
  752. priv->magic_not_rop_nr = 0x03;
  753. priv->magicgpc918 = 0x00100000;
  754. break;
  755. }
  756. if (!priv->magic_not_rop_nr) {
  757. NV_ERROR(dev, "PGRAPH: unknown config: %d/%d/%d/%d, %d\n",
  758. priv->tp_nr[0], priv->tp_nr[1], priv->tp_nr[2],
  759. priv->tp_nr[3], priv->rop_nr);
  760. /* use 0xc3's values... */
  761. priv->magic_not_rop_nr = 0x03;
  762. priv->magicgpc918 = 0x00200000;
  763. }
  764. NVOBJ_CLASS(dev, 0x902d, GR); /* 2D */
  765. NVOBJ_CLASS(dev, 0x9039, GR); /* M2MF */
  766. NVOBJ_MTHD (dev, 0x9039, 0x0500, nvc0_graph_mthd_page_flip);
  767. NVOBJ_CLASS(dev, 0x9097, GR); /* 3D */
  768. if (fermi >= 0x9197)
  769. NVOBJ_CLASS(dev, 0x9197, GR); /* 3D (NVC1-) */
  770. if (fermi >= 0x9297)
  771. NVOBJ_CLASS(dev, 0x9297, GR); /* 3D (NVC8-) */
  772. NVOBJ_CLASS(dev, 0x90c0, GR); /* COMPUTE */
  773. return 0;
  774. error:
  775. nvc0_graph_destroy(dev, NVOBJ_ENGINE_GR);
  776. return ret;
  777. }
  778. MODULE_FIRMWARE("nouveau/nvc0_fuc409c");
  779. MODULE_FIRMWARE("nouveau/nvc0_fuc409d");
  780. MODULE_FIRMWARE("nouveau/nvc0_fuc41ac");
  781. MODULE_FIRMWARE("nouveau/nvc0_fuc41ad");
  782. MODULE_FIRMWARE("nouveau/nvc3_fuc409c");
  783. MODULE_FIRMWARE("nouveau/nvc3_fuc409d");
  784. MODULE_FIRMWARE("nouveau/nvc3_fuc41ac");
  785. MODULE_FIRMWARE("nouveau/nvc3_fuc41ad");
  786. MODULE_FIRMWARE("nouveau/nvc4_fuc409c");
  787. MODULE_FIRMWARE("nouveau/nvc4_fuc409d");
  788. MODULE_FIRMWARE("nouveau/nvc4_fuc41ac");
  789. MODULE_FIRMWARE("nouveau/nvc4_fuc41ad");
  790. MODULE_FIRMWARE("nouveau/fuc409c");
  791. MODULE_FIRMWARE("nouveau/fuc409d");
  792. MODULE_FIRMWARE("nouveau/fuc41ac");
  793. MODULE_FIRMWARE("nouveau/fuc41ad");