nouveau_object.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Copyright (C) 2006 Ben Skeggs.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial
  16. * portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. */
  27. /*
  28. * Authors:
  29. * Ben Skeggs <darktama@iinet.net.au>
  30. */
  31. #include "drmP.h"
  32. #include "drm.h"
  33. #include "nouveau_drv.h"
  34. #include "nouveau_drm.h"
  35. #include "nouveau_fifo.h"
  36. #include "nouveau_ramht.h"
  37. #include "nouveau_software.h"
  38. #include "nouveau_vm.h"
  39. struct nouveau_gpuobj_method {
  40. struct list_head head;
  41. u32 mthd;
  42. int (*exec)(struct nouveau_channel *, u32 class, u32 mthd, u32 data);
  43. };
  44. struct nouveau_gpuobj_class {
  45. struct list_head head;
  46. struct list_head methods;
  47. u32 id;
  48. u32 engine;
  49. };
  50. int
  51. nouveau_gpuobj_class_new(struct drm_device *dev, u32 class, u32 engine)
  52. {
  53. struct drm_nouveau_private *dev_priv = dev->dev_private;
  54. struct nouveau_gpuobj_class *oc;
  55. oc = kzalloc(sizeof(*oc), GFP_KERNEL);
  56. if (!oc)
  57. return -ENOMEM;
  58. INIT_LIST_HEAD(&oc->methods);
  59. oc->id = class;
  60. oc->engine = engine;
  61. list_add(&oc->head, &dev_priv->classes);
  62. return 0;
  63. }
  64. int
  65. nouveau_gpuobj_mthd_new(struct drm_device *dev, u32 class, u32 mthd,
  66. int (*exec)(struct nouveau_channel *, u32, u32, u32))
  67. {
  68. struct drm_nouveau_private *dev_priv = dev->dev_private;
  69. struct nouveau_gpuobj_method *om;
  70. struct nouveau_gpuobj_class *oc;
  71. list_for_each_entry(oc, &dev_priv->classes, head) {
  72. if (oc->id == class)
  73. goto found;
  74. }
  75. return -EINVAL;
  76. found:
  77. om = kzalloc(sizeof(*om), GFP_KERNEL);
  78. if (!om)
  79. return -ENOMEM;
  80. om->mthd = mthd;
  81. om->exec = exec;
  82. list_add(&om->head, &oc->methods);
  83. return 0;
  84. }
  85. int
  86. nouveau_gpuobj_mthd_call(struct nouveau_channel *chan,
  87. u32 class, u32 mthd, u32 data)
  88. {
  89. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  90. struct nouveau_gpuobj_method *om;
  91. struct nouveau_gpuobj_class *oc;
  92. list_for_each_entry(oc, &dev_priv->classes, head) {
  93. if (oc->id != class)
  94. continue;
  95. list_for_each_entry(om, &oc->methods, head) {
  96. if (om->mthd == mthd)
  97. return om->exec(chan, class, mthd, data);
  98. }
  99. }
  100. return -ENOENT;
  101. }
  102. int
  103. nouveau_gpuobj_mthd_call2(struct drm_device *dev, int chid,
  104. u32 class, u32 mthd, u32 data)
  105. {
  106. struct drm_nouveau_private *dev_priv = dev->dev_private;
  107. struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  108. struct nouveau_channel *chan = NULL;
  109. unsigned long flags;
  110. int ret = -EINVAL;
  111. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  112. if (chid >= 0 && chid < pfifo->channels)
  113. chan = dev_priv->channels.ptr[chid];
  114. if (chan)
  115. ret = nouveau_gpuobj_mthd_call(chan, class, mthd, data);
  116. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  117. return ret;
  118. }
  119. int
  120. nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
  121. uint32_t size, int align, uint32_t flags,
  122. struct nouveau_gpuobj **gpuobj_ret)
  123. {
  124. struct drm_nouveau_private *dev_priv = dev->dev_private;
  125. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  126. struct nouveau_gpuobj *gpuobj;
  127. struct drm_mm_node *ramin = NULL;
  128. int ret, i;
  129. NV_DEBUG(dev, "ch%d size=%u align=%d flags=0x%08x\n",
  130. chan ? chan->id : -1, size, align, flags);
  131. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  132. if (!gpuobj)
  133. return -ENOMEM;
  134. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  135. gpuobj->dev = dev;
  136. gpuobj->flags = flags;
  137. kref_init(&gpuobj->refcount);
  138. gpuobj->size = size;
  139. spin_lock(&dev_priv->ramin_lock);
  140. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  141. spin_unlock(&dev_priv->ramin_lock);
  142. if (!(flags & NVOBJ_FLAG_VM) && chan) {
  143. ramin = drm_mm_search_free(&chan->ramin_heap, size, align, 0);
  144. if (ramin)
  145. ramin = drm_mm_get_block(ramin, size, align);
  146. if (!ramin) {
  147. nouveau_gpuobj_ref(NULL, &gpuobj);
  148. return -ENOMEM;
  149. }
  150. gpuobj->pinst = chan->ramin->pinst;
  151. if (gpuobj->pinst != ~0)
  152. gpuobj->pinst += ramin->start;
  153. gpuobj->cinst = ramin->start;
  154. gpuobj->vinst = ramin->start + chan->ramin->vinst;
  155. gpuobj->node = ramin;
  156. } else {
  157. ret = instmem->get(gpuobj, chan, size, align);
  158. if (ret) {
  159. nouveau_gpuobj_ref(NULL, &gpuobj);
  160. return ret;
  161. }
  162. ret = -ENOSYS;
  163. if (!(flags & NVOBJ_FLAG_DONT_MAP))
  164. ret = instmem->map(gpuobj);
  165. if (ret)
  166. gpuobj->pinst = ~0;
  167. gpuobj->cinst = NVOBJ_CINST_GLOBAL;
  168. }
  169. if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
  170. for (i = 0; i < gpuobj->size; i += 4)
  171. nv_wo32(gpuobj, i, 0);
  172. instmem->flush(dev);
  173. }
  174. *gpuobj_ret = gpuobj;
  175. return 0;
  176. }
  177. int
  178. nouveau_gpuobj_init(struct drm_device *dev)
  179. {
  180. struct drm_nouveau_private *dev_priv = dev->dev_private;
  181. NV_DEBUG(dev, "\n");
  182. INIT_LIST_HEAD(&dev_priv->gpuobj_list);
  183. INIT_LIST_HEAD(&dev_priv->classes);
  184. spin_lock_init(&dev_priv->ramin_lock);
  185. dev_priv->ramin_base = ~0;
  186. return 0;
  187. }
  188. void
  189. nouveau_gpuobj_takedown(struct drm_device *dev)
  190. {
  191. struct drm_nouveau_private *dev_priv = dev->dev_private;
  192. struct nouveau_gpuobj_method *om, *tm;
  193. struct nouveau_gpuobj_class *oc, *tc;
  194. NV_DEBUG(dev, "\n");
  195. list_for_each_entry_safe(oc, tc, &dev_priv->classes, head) {
  196. list_for_each_entry_safe(om, tm, &oc->methods, head) {
  197. list_del(&om->head);
  198. kfree(om);
  199. }
  200. list_del(&oc->head);
  201. kfree(oc);
  202. }
  203. WARN_ON(!list_empty(&dev_priv->gpuobj_list));
  204. }
  205. static void
  206. nouveau_gpuobj_del(struct kref *ref)
  207. {
  208. struct nouveau_gpuobj *gpuobj =
  209. container_of(ref, struct nouveau_gpuobj, refcount);
  210. struct drm_device *dev = gpuobj->dev;
  211. struct drm_nouveau_private *dev_priv = dev->dev_private;
  212. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  213. int i;
  214. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  215. if (gpuobj->node && (gpuobj->flags & NVOBJ_FLAG_ZERO_FREE)) {
  216. for (i = 0; i < gpuobj->size; i += 4)
  217. nv_wo32(gpuobj, i, 0);
  218. instmem->flush(dev);
  219. }
  220. if (gpuobj->dtor)
  221. gpuobj->dtor(dev, gpuobj);
  222. if (gpuobj->cinst == NVOBJ_CINST_GLOBAL) {
  223. if (gpuobj->node) {
  224. instmem->unmap(gpuobj);
  225. instmem->put(gpuobj);
  226. }
  227. } else {
  228. if (gpuobj->node) {
  229. spin_lock(&dev_priv->ramin_lock);
  230. drm_mm_put_block(gpuobj->node);
  231. spin_unlock(&dev_priv->ramin_lock);
  232. }
  233. }
  234. spin_lock(&dev_priv->ramin_lock);
  235. list_del(&gpuobj->list);
  236. spin_unlock(&dev_priv->ramin_lock);
  237. kfree(gpuobj);
  238. }
  239. void
  240. nouveau_gpuobj_ref(struct nouveau_gpuobj *ref, struct nouveau_gpuobj **ptr)
  241. {
  242. if (ref)
  243. kref_get(&ref->refcount);
  244. if (*ptr)
  245. kref_put(&(*ptr)->refcount, nouveau_gpuobj_del);
  246. *ptr = ref;
  247. }
  248. int
  249. nouveau_gpuobj_new_fake(struct drm_device *dev, u32 pinst, u64 vinst,
  250. u32 size, u32 flags, struct nouveau_gpuobj **pgpuobj)
  251. {
  252. struct drm_nouveau_private *dev_priv = dev->dev_private;
  253. struct nouveau_gpuobj *gpuobj = NULL;
  254. int i;
  255. NV_DEBUG(dev,
  256. "pinst=0x%08x vinst=0x%010llx size=0x%08x flags=0x%08x\n",
  257. pinst, vinst, size, flags);
  258. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  259. if (!gpuobj)
  260. return -ENOMEM;
  261. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  262. gpuobj->dev = dev;
  263. gpuobj->flags = flags;
  264. kref_init(&gpuobj->refcount);
  265. gpuobj->size = size;
  266. gpuobj->pinst = pinst;
  267. gpuobj->cinst = NVOBJ_CINST_GLOBAL;
  268. gpuobj->vinst = vinst;
  269. if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
  270. for (i = 0; i < gpuobj->size; i += 4)
  271. nv_wo32(gpuobj, i, 0);
  272. dev_priv->engine.instmem.flush(dev);
  273. }
  274. spin_lock(&dev_priv->ramin_lock);
  275. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  276. spin_unlock(&dev_priv->ramin_lock);
  277. *pgpuobj = gpuobj;
  278. return 0;
  279. }
  280. void
  281. nv50_gpuobj_dma_init(struct nouveau_gpuobj *obj, u32 offset, int class,
  282. u64 base, u64 size, int target, int access,
  283. u32 type, u32 comp)
  284. {
  285. struct drm_nouveau_private *dev_priv = obj->dev->dev_private;
  286. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  287. u32 flags0;
  288. flags0 = (comp << 29) | (type << 22) | class;
  289. flags0 |= 0x00100000;
  290. switch (access) {
  291. case NV_MEM_ACCESS_RO: flags0 |= 0x00040000; break;
  292. case NV_MEM_ACCESS_RW:
  293. case NV_MEM_ACCESS_WO: flags0 |= 0x00080000; break;
  294. default:
  295. break;
  296. }
  297. switch (target) {
  298. case NV_MEM_TARGET_VRAM:
  299. flags0 |= 0x00010000;
  300. break;
  301. case NV_MEM_TARGET_PCI:
  302. flags0 |= 0x00020000;
  303. break;
  304. case NV_MEM_TARGET_PCI_NOSNOOP:
  305. flags0 |= 0x00030000;
  306. break;
  307. case NV_MEM_TARGET_GART:
  308. base += dev_priv->gart_info.aper_base;
  309. default:
  310. flags0 &= ~0x00100000;
  311. break;
  312. }
  313. /* convert to base + limit */
  314. size = (base + size) - 1;
  315. nv_wo32(obj, offset + 0x00, flags0);
  316. nv_wo32(obj, offset + 0x04, lower_32_bits(size));
  317. nv_wo32(obj, offset + 0x08, lower_32_bits(base));
  318. nv_wo32(obj, offset + 0x0c, upper_32_bits(size) << 24 |
  319. upper_32_bits(base));
  320. nv_wo32(obj, offset + 0x10, 0x00000000);
  321. nv_wo32(obj, offset + 0x14, 0x00000000);
  322. pinstmem->flush(obj->dev);
  323. }
  324. int
  325. nv50_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base, u64 size,
  326. int target, int access, u32 type, u32 comp,
  327. struct nouveau_gpuobj **pobj)
  328. {
  329. struct drm_device *dev = chan->dev;
  330. int ret;
  331. ret = nouveau_gpuobj_new(dev, chan, 24, 16, NVOBJ_FLAG_ZERO_FREE, pobj);
  332. if (ret)
  333. return ret;
  334. nv50_gpuobj_dma_init(*pobj, 0, class, base, size, target,
  335. access, type, comp);
  336. return 0;
  337. }
  338. int
  339. nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base,
  340. u64 size, int access, int target,
  341. struct nouveau_gpuobj **pobj)
  342. {
  343. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  344. struct drm_device *dev = chan->dev;
  345. struct nouveau_gpuobj *obj;
  346. u32 flags0, flags2;
  347. int ret;
  348. if (dev_priv->card_type >= NV_50) {
  349. u32 comp = (target == NV_MEM_TARGET_VM) ? NV_MEM_COMP_VM : 0;
  350. u32 type = (target == NV_MEM_TARGET_VM) ? NV_MEM_TYPE_VM : 0;
  351. return nv50_gpuobj_dma_new(chan, class, base, size,
  352. target, access, type, comp, pobj);
  353. }
  354. if (target == NV_MEM_TARGET_GART) {
  355. struct nouveau_gpuobj *gart = dev_priv->gart_info.sg_ctxdma;
  356. if (dev_priv->gart_info.type == NOUVEAU_GART_PDMA) {
  357. if (base == 0) {
  358. nouveau_gpuobj_ref(gart, pobj);
  359. return 0;
  360. }
  361. base = nouveau_sgdma_get_physical(dev, base);
  362. target = NV_MEM_TARGET_PCI;
  363. } else {
  364. base += dev_priv->gart_info.aper_base;
  365. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP)
  366. target = NV_MEM_TARGET_PCI_NOSNOOP;
  367. else
  368. target = NV_MEM_TARGET_PCI;
  369. }
  370. }
  371. flags0 = class;
  372. flags0 |= 0x00003000; /* PT present, PT linear */
  373. flags2 = 0;
  374. switch (target) {
  375. case NV_MEM_TARGET_PCI:
  376. flags0 |= 0x00020000;
  377. break;
  378. case NV_MEM_TARGET_PCI_NOSNOOP:
  379. flags0 |= 0x00030000;
  380. break;
  381. default:
  382. break;
  383. }
  384. switch (access) {
  385. case NV_MEM_ACCESS_RO:
  386. flags0 |= 0x00004000;
  387. break;
  388. case NV_MEM_ACCESS_WO:
  389. flags0 |= 0x00008000;
  390. default:
  391. flags2 |= 0x00000002;
  392. break;
  393. }
  394. flags0 |= (base & 0x00000fff) << 20;
  395. flags2 |= (base & 0xfffff000);
  396. ret = nouveau_gpuobj_new(dev, chan, 16, 16, NVOBJ_FLAG_ZERO_FREE, &obj);
  397. if (ret)
  398. return ret;
  399. nv_wo32(obj, 0x00, flags0);
  400. nv_wo32(obj, 0x04, size - 1);
  401. nv_wo32(obj, 0x08, flags2);
  402. nv_wo32(obj, 0x0c, flags2);
  403. obj->engine = NVOBJ_ENGINE_SW;
  404. obj->class = class;
  405. *pobj = obj;
  406. return 0;
  407. }
  408. int
  409. nouveau_gpuobj_gr_new(struct nouveau_channel *chan, u32 handle, int class)
  410. {
  411. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  412. struct drm_device *dev = chan->dev;
  413. struct nouveau_gpuobj_class *oc;
  414. int ret;
  415. NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
  416. list_for_each_entry(oc, &dev_priv->classes, head) {
  417. struct nouveau_exec_engine *eng = dev_priv->eng[oc->engine];
  418. if (oc->id != class)
  419. continue;
  420. if (!chan->engctx[oc->engine]) {
  421. ret = eng->context_new(chan, oc->engine);
  422. if (ret)
  423. return ret;
  424. }
  425. return eng->object_new(chan, oc->engine, handle, class);
  426. }
  427. return -EINVAL;
  428. }
  429. static int
  430. nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
  431. {
  432. struct drm_device *dev = chan->dev;
  433. struct drm_nouveau_private *dev_priv = dev->dev_private;
  434. uint32_t size;
  435. uint32_t base;
  436. int ret;
  437. NV_DEBUG(dev, "ch%d\n", chan->id);
  438. /* Base amount for object storage (4KiB enough?) */
  439. size = 0x2000;
  440. base = 0;
  441. if (dev_priv->card_type == NV_50) {
  442. /* Various fixed table thingos */
  443. size += 0x1400; /* mostly unknown stuff */
  444. size += 0x4000; /* vm pd */
  445. base = 0x6000;
  446. /* RAMHT, not sure about setting size yet, 32KiB to be safe */
  447. size += 0x8000;
  448. /* RAMFC */
  449. size += 0x1000;
  450. }
  451. ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
  452. if (ret) {
  453. NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
  454. return ret;
  455. }
  456. ret = drm_mm_init(&chan->ramin_heap, base, size - base);
  457. if (ret) {
  458. NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
  459. nouveau_gpuobj_ref(NULL, &chan->ramin);
  460. return ret;
  461. }
  462. return 0;
  463. }
  464. static int
  465. nvc0_gpuobj_channel_init(struct nouveau_channel *chan, struct nouveau_vm *vm)
  466. {
  467. struct drm_device *dev = chan->dev;
  468. struct nouveau_gpuobj *pgd = NULL;
  469. struct nouveau_vm_pgd *vpgd;
  470. int ret;
  471. ret = nouveau_gpuobj_new(dev, NULL, 4096, 0x1000, 0, &chan->ramin);
  472. if (ret)
  473. return ret;
  474. /* create page directory for this vm if none currently exists,
  475. * will be destroyed automagically when last reference to the
  476. * vm is removed
  477. */
  478. if (list_empty(&vm->pgd_list)) {
  479. ret = nouveau_gpuobj_new(dev, NULL, 65536, 0x1000, 0, &pgd);
  480. if (ret)
  481. return ret;
  482. }
  483. nouveau_vm_ref(vm, &chan->vm, pgd);
  484. nouveau_gpuobj_ref(NULL, &pgd);
  485. /* point channel at vm's page directory */
  486. vpgd = list_first_entry(&vm->pgd_list, struct nouveau_vm_pgd, head);
  487. nv_wo32(chan->ramin, 0x0200, lower_32_bits(vpgd->obj->vinst));
  488. nv_wo32(chan->ramin, 0x0204, upper_32_bits(vpgd->obj->vinst));
  489. nv_wo32(chan->ramin, 0x0208, 0xffffffff);
  490. nv_wo32(chan->ramin, 0x020c, 0x000000ff);
  491. return 0;
  492. }
  493. int
  494. nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
  495. uint32_t vram_h, uint32_t tt_h)
  496. {
  497. struct drm_device *dev = chan->dev;
  498. struct drm_nouveau_private *dev_priv = dev->dev_private;
  499. struct nouveau_fpriv *fpriv = nouveau_fpriv(chan->file_priv);
  500. struct nouveau_vm *vm = fpriv ? fpriv->vm : dev_priv->chan_vm;
  501. struct nouveau_gpuobj *vram = NULL, *tt = NULL;
  502. int ret;
  503. NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
  504. if (dev_priv->card_type >= NV_C0)
  505. return nvc0_gpuobj_channel_init(chan, vm);
  506. /* Allocate a chunk of memory for per-channel object storage */
  507. ret = nouveau_gpuobj_channel_init_pramin(chan);
  508. if (ret) {
  509. NV_ERROR(dev, "init pramin\n");
  510. return ret;
  511. }
  512. /* NV50 VM
  513. * - Allocate per-channel page-directory
  514. * - Link with shared channel VM
  515. */
  516. if (vm) {
  517. u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
  518. u64 vm_vinst = chan->ramin->vinst + pgd_offs;
  519. u32 vm_pinst = chan->ramin->pinst;
  520. if (vm_pinst != ~0)
  521. vm_pinst += pgd_offs;
  522. ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
  523. 0, &chan->vm_pd);
  524. if (ret)
  525. return ret;
  526. nouveau_vm_ref(vm, &chan->vm, chan->vm_pd);
  527. }
  528. /* RAMHT */
  529. if (dev_priv->card_type < NV_50) {
  530. nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
  531. } else {
  532. struct nouveau_gpuobj *ramht = NULL;
  533. ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
  534. NVOBJ_FLAG_ZERO_ALLOC, &ramht);
  535. if (ret)
  536. return ret;
  537. ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
  538. nouveau_gpuobj_ref(NULL, &ramht);
  539. if (ret)
  540. return ret;
  541. }
  542. /* VRAM ctxdma */
  543. if (dev_priv->card_type >= NV_50) {
  544. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  545. 0, (1ULL << 40), NV_MEM_ACCESS_RW,
  546. NV_MEM_TARGET_VM, &vram);
  547. if (ret) {
  548. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  549. return ret;
  550. }
  551. } else {
  552. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  553. 0, dev_priv->fb_available_size,
  554. NV_MEM_ACCESS_RW,
  555. NV_MEM_TARGET_VRAM, &vram);
  556. if (ret) {
  557. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  558. return ret;
  559. }
  560. }
  561. ret = nouveau_ramht_insert(chan, vram_h, vram);
  562. nouveau_gpuobj_ref(NULL, &vram);
  563. if (ret) {
  564. NV_ERROR(dev, "Error adding VRAM ctxdma to RAMHT: %d\n", ret);
  565. return ret;
  566. }
  567. /* TT memory ctxdma */
  568. if (dev_priv->card_type >= NV_50) {
  569. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  570. 0, (1ULL << 40), NV_MEM_ACCESS_RW,
  571. NV_MEM_TARGET_VM, &tt);
  572. } else {
  573. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  574. 0, dev_priv->gart_info.aper_size,
  575. NV_MEM_ACCESS_RW,
  576. NV_MEM_TARGET_GART, &tt);
  577. }
  578. if (ret) {
  579. NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
  580. return ret;
  581. }
  582. ret = nouveau_ramht_insert(chan, tt_h, tt);
  583. nouveau_gpuobj_ref(NULL, &tt);
  584. if (ret) {
  585. NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
  586. return ret;
  587. }
  588. return 0;
  589. }
  590. void
  591. nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
  592. {
  593. NV_DEBUG(chan->dev, "ch%d\n", chan->id);
  594. nouveau_vm_ref(NULL, &chan->vm, chan->vm_pd);
  595. nouveau_gpuobj_ref(NULL, &chan->vm_pd);
  596. if (drm_mm_initialized(&chan->ramin_heap))
  597. drm_mm_takedown(&chan->ramin_heap);
  598. nouveau_gpuobj_ref(NULL, &chan->ramin);
  599. }
  600. int
  601. nouveau_gpuobj_suspend(struct drm_device *dev)
  602. {
  603. struct drm_nouveau_private *dev_priv = dev->dev_private;
  604. struct nouveau_gpuobj *gpuobj;
  605. int i;
  606. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  607. if (gpuobj->cinst != NVOBJ_CINST_GLOBAL)
  608. continue;
  609. gpuobj->suspend = vmalloc(gpuobj->size);
  610. if (!gpuobj->suspend) {
  611. nouveau_gpuobj_resume(dev);
  612. return -ENOMEM;
  613. }
  614. for (i = 0; i < gpuobj->size; i += 4)
  615. gpuobj->suspend[i/4] = nv_ro32(gpuobj, i);
  616. }
  617. return 0;
  618. }
  619. void
  620. nouveau_gpuobj_resume(struct drm_device *dev)
  621. {
  622. struct drm_nouveau_private *dev_priv = dev->dev_private;
  623. struct nouveau_gpuobj *gpuobj;
  624. int i;
  625. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  626. if (!gpuobj->suspend)
  627. continue;
  628. for (i = 0; i < gpuobj->size; i += 4)
  629. nv_wo32(gpuobj, i, gpuobj->suspend[i/4]);
  630. vfree(gpuobj->suspend);
  631. gpuobj->suspend = NULL;
  632. }
  633. dev_priv->engine.instmem.flush(dev);
  634. }
  635. int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
  636. struct drm_file *file_priv)
  637. {
  638. struct drm_nouveau_grobj_alloc *init = data;
  639. struct nouveau_channel *chan;
  640. int ret;
  641. if (init->handle == ~0)
  642. return -EINVAL;
  643. /* compatibility with userspace that assumes 506e for all chipsets */
  644. if (init->class == 0x506e) {
  645. init->class = nouveau_software_class(dev);
  646. if (init->class == 0x906e)
  647. return 0;
  648. } else
  649. if (init->class == 0x906e) {
  650. NV_ERROR(dev, "906e not supported yet\n");
  651. return -EINVAL;
  652. }
  653. chan = nouveau_channel_get(file_priv, init->channel);
  654. if (IS_ERR(chan))
  655. return PTR_ERR(chan);
  656. if (nouveau_ramht_find(chan, init->handle)) {
  657. ret = -EEXIST;
  658. goto out;
  659. }
  660. ret = nouveau_gpuobj_gr_new(chan, init->handle, init->class);
  661. if (ret) {
  662. NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
  663. ret, init->channel, init->handle);
  664. }
  665. out:
  666. nouveau_channel_put(&chan);
  667. return ret;
  668. }
  669. int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
  670. struct drm_file *file_priv)
  671. {
  672. struct drm_nouveau_gpuobj_free *objfree = data;
  673. struct nouveau_channel *chan;
  674. int ret;
  675. chan = nouveau_channel_get(file_priv, objfree->channel);
  676. if (IS_ERR(chan))
  677. return PTR_ERR(chan);
  678. /* Synchronize with the user channel */
  679. nouveau_channel_idle(chan);
  680. ret = nouveau_ramht_remove(chan, objfree->handle);
  681. nouveau_channel_put(&chan);
  682. return ret;
  683. }
  684. u32
  685. nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
  686. {
  687. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  688. struct drm_device *dev = gpuobj->dev;
  689. unsigned long flags;
  690. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  691. u64 ptr = gpuobj->vinst + offset;
  692. u32 base = ptr >> 16;
  693. u32 val;
  694. spin_lock_irqsave(&dev_priv->vm_lock, flags);
  695. if (dev_priv->ramin_base != base) {
  696. dev_priv->ramin_base = base;
  697. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  698. }
  699. val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
  700. spin_unlock_irqrestore(&dev_priv->vm_lock, flags);
  701. return val;
  702. }
  703. return nv_ri32(dev, gpuobj->pinst + offset);
  704. }
  705. void
  706. nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
  707. {
  708. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  709. struct drm_device *dev = gpuobj->dev;
  710. unsigned long flags;
  711. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  712. u64 ptr = gpuobj->vinst + offset;
  713. u32 base = ptr >> 16;
  714. spin_lock_irqsave(&dev_priv->vm_lock, flags);
  715. if (dev_priv->ramin_base != base) {
  716. dev_priv->ramin_base = base;
  717. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  718. }
  719. nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
  720. spin_unlock_irqrestore(&dev_priv->vm_lock, flags);
  721. return;
  722. }
  723. nv_wi32(dev, gpuobj->pinst + offset, val);
  724. }