nvc0_graph.c 25 KB

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