nvc0_graph.c 25 KB

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