mga_dma.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /* mga_dma.c -- DMA support for mga g200/g400 -*- linux-c -*-
  2. * Created: Mon Dec 13 01:50:01 1999 by jhartmann@precisioninsight.com
  3. *
  4. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  5. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  6. * All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the next
  16. * paragraph) shall be included in all copies or substantial portions of the
  17. * Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. */
  27. /**
  28. * \file mga_dma.c
  29. * DMA support for MGA G200 / G400.
  30. *
  31. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  32. * \author Jeff Hartmann <jhartmann@valinux.com>
  33. * \author Keith Whitwell <keith@tungstengraphics.com>
  34. * \author Gareth Hughes <gareth@valinux.com>
  35. */
  36. #include <drm/drmP.h>
  37. #include <drm/mga_drm.h>
  38. #include "mga_drv.h"
  39. #define MGA_DEFAULT_USEC_TIMEOUT 10000
  40. #define MGA_FREELIST_DEBUG 0
  41. #define MINIMAL_CLEANUP 0
  42. #define FULL_CLEANUP 1
  43. static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup);
  44. /* ================================================================
  45. * Engine control
  46. */
  47. int mga_do_wait_for_idle(drm_mga_private_t *dev_priv)
  48. {
  49. u32 status = 0;
  50. int i;
  51. DRM_DEBUG("\n");
  52. for (i = 0; i < dev_priv->usec_timeout; i++) {
  53. status = MGA_READ(MGA_STATUS) & MGA_ENGINE_IDLE_MASK;
  54. if (status == MGA_ENDPRDMASTS) {
  55. MGA_WRITE8(MGA_CRTC_INDEX, 0);
  56. return 0;
  57. }
  58. DRM_UDELAY(1);
  59. }
  60. #if MGA_DMA_DEBUG
  61. DRM_ERROR("failed!\n");
  62. DRM_INFO(" status=0x%08x\n", status);
  63. #endif
  64. return -EBUSY;
  65. }
  66. static int mga_do_dma_reset(drm_mga_private_t *dev_priv)
  67. {
  68. drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
  69. drm_mga_primary_buffer_t *primary = &dev_priv->prim;
  70. DRM_DEBUG("\n");
  71. /* The primary DMA stream should look like new right about now.
  72. */
  73. primary->tail = 0;
  74. primary->space = primary->size;
  75. primary->last_flush = 0;
  76. sarea_priv->last_wrap = 0;
  77. /* FIXME: Reset counters, buffer ages etc...
  78. */
  79. /* FIXME: What else do we need to reinitialize? WARP stuff?
  80. */
  81. return 0;
  82. }
  83. /* ================================================================
  84. * Primary DMA stream
  85. */
  86. void mga_do_dma_flush(drm_mga_private_t *dev_priv)
  87. {
  88. drm_mga_primary_buffer_t *primary = &dev_priv->prim;
  89. u32 head, tail;
  90. u32 status = 0;
  91. int i;
  92. DMA_LOCALS;
  93. DRM_DEBUG("\n");
  94. /* We need to wait so that we can do an safe flush */
  95. for (i = 0; i < dev_priv->usec_timeout; i++) {
  96. status = MGA_READ(MGA_STATUS) & MGA_ENGINE_IDLE_MASK;
  97. if (status == MGA_ENDPRDMASTS)
  98. break;
  99. DRM_UDELAY(1);
  100. }
  101. if (primary->tail == primary->last_flush) {
  102. DRM_DEBUG(" bailing out...\n");
  103. return;
  104. }
  105. tail = primary->tail + dev_priv->primary->offset;
  106. /* We need to pad the stream between flushes, as the card
  107. * actually (partially?) reads the first of these commands.
  108. * See page 4-16 in the G400 manual, middle of the page or so.
  109. */
  110. BEGIN_DMA(1);
  111. DMA_BLOCK(MGA_DMAPAD, 0x00000000,
  112. MGA_DMAPAD, 0x00000000,
  113. MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
  114. ADVANCE_DMA();
  115. primary->last_flush = primary->tail;
  116. head = MGA_READ(MGA_PRIMADDRESS);
  117. if (head <= tail)
  118. primary->space = primary->size - primary->tail;
  119. else
  120. primary->space = head - tail;
  121. DRM_DEBUG(" head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
  122. DRM_DEBUG(" tail = 0x%06lx\n", (unsigned long)(tail - dev_priv->primary->offset));
  123. DRM_DEBUG(" space = 0x%06x\n", primary->space);
  124. mga_flush_write_combine();
  125. MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
  126. DRM_DEBUG("done.\n");
  127. }
  128. void mga_do_dma_wrap_start(drm_mga_private_t *dev_priv)
  129. {
  130. drm_mga_primary_buffer_t *primary = &dev_priv->prim;
  131. u32 head, tail;
  132. DMA_LOCALS;
  133. DRM_DEBUG("\n");
  134. BEGIN_DMA_WRAP();
  135. DMA_BLOCK(MGA_DMAPAD, 0x00000000,
  136. MGA_DMAPAD, 0x00000000,
  137. MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
  138. ADVANCE_DMA();
  139. tail = primary->tail + dev_priv->primary->offset;
  140. primary->tail = 0;
  141. primary->last_flush = 0;
  142. primary->last_wrap++;
  143. head = MGA_READ(MGA_PRIMADDRESS);
  144. if (head == dev_priv->primary->offset)
  145. primary->space = primary->size;
  146. else
  147. primary->space = head - dev_priv->primary->offset;
  148. DRM_DEBUG(" head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
  149. DRM_DEBUG(" tail = 0x%06x\n", primary->tail);
  150. DRM_DEBUG(" wrap = %d\n", primary->last_wrap);
  151. DRM_DEBUG(" space = 0x%06x\n", primary->space);
  152. mga_flush_write_combine();
  153. MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
  154. set_bit(0, &primary->wrapped);
  155. DRM_DEBUG("done.\n");
  156. }
  157. void mga_do_dma_wrap_end(drm_mga_private_t *dev_priv)
  158. {
  159. drm_mga_primary_buffer_t *primary = &dev_priv->prim;
  160. drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
  161. u32 head = dev_priv->primary->offset;
  162. DRM_DEBUG("\n");
  163. sarea_priv->last_wrap++;
  164. DRM_DEBUG(" wrap = %d\n", sarea_priv->last_wrap);
  165. mga_flush_write_combine();
  166. MGA_WRITE(MGA_PRIMADDRESS, head | MGA_DMA_GENERAL);
  167. clear_bit(0, &primary->wrapped);
  168. DRM_DEBUG("done.\n");
  169. }
  170. /* ================================================================
  171. * Freelist management
  172. */
  173. #define MGA_BUFFER_USED (~0)
  174. #define MGA_BUFFER_FREE 0
  175. #if MGA_FREELIST_DEBUG
  176. static void mga_freelist_print(struct drm_device *dev)
  177. {
  178. drm_mga_private_t *dev_priv = dev->dev_private;
  179. drm_mga_freelist_t *entry;
  180. DRM_INFO("\n");
  181. DRM_INFO("current dispatch: last=0x%x done=0x%x\n",
  182. dev_priv->sarea_priv->last_dispatch,
  183. (unsigned int)(MGA_READ(MGA_PRIMADDRESS) -
  184. dev_priv->primary->offset));
  185. DRM_INFO("current freelist:\n");
  186. for (entry = dev_priv->head->next; entry; entry = entry->next) {
  187. DRM_INFO(" %p idx=%2d age=0x%x 0x%06lx\n",
  188. entry, entry->buf->idx, entry->age.head,
  189. (unsigned long)(entry->age.head - dev_priv->primary->offset));
  190. }
  191. DRM_INFO("\n");
  192. }
  193. #endif
  194. static int mga_freelist_init(struct drm_device *dev, drm_mga_private_t *dev_priv)
  195. {
  196. struct drm_device_dma *dma = dev->dma;
  197. struct drm_buf *buf;
  198. drm_mga_buf_priv_t *buf_priv;
  199. drm_mga_freelist_t *entry;
  200. int i;
  201. DRM_DEBUG("count=%d\n", dma->buf_count);
  202. dev_priv->head = kzalloc(sizeof(drm_mga_freelist_t), GFP_KERNEL);
  203. if (dev_priv->head == NULL)
  204. return -ENOMEM;
  205. SET_AGE(&dev_priv->head->age, MGA_BUFFER_USED, 0);
  206. for (i = 0; i < dma->buf_count; i++) {
  207. buf = dma->buflist[i];
  208. buf_priv = buf->dev_private;
  209. entry = kzalloc(sizeof(drm_mga_freelist_t), GFP_KERNEL);
  210. if (entry == NULL)
  211. return -ENOMEM;
  212. entry->next = dev_priv->head->next;
  213. entry->prev = dev_priv->head;
  214. SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
  215. entry->buf = buf;
  216. if (dev_priv->head->next != NULL)
  217. dev_priv->head->next->prev = entry;
  218. if (entry->next == NULL)
  219. dev_priv->tail = entry;
  220. buf_priv->list_entry = entry;
  221. buf_priv->discard = 0;
  222. buf_priv->dispatched = 0;
  223. dev_priv->head->next = entry;
  224. }
  225. return 0;
  226. }
  227. static void mga_freelist_cleanup(struct drm_device *dev)
  228. {
  229. drm_mga_private_t *dev_priv = dev->dev_private;
  230. drm_mga_freelist_t *entry;
  231. drm_mga_freelist_t *next;
  232. DRM_DEBUG("\n");
  233. entry = dev_priv->head;
  234. while (entry) {
  235. next = entry->next;
  236. kfree(entry);
  237. entry = next;
  238. }
  239. dev_priv->head = dev_priv->tail = NULL;
  240. }
  241. #if 0
  242. /* FIXME: Still needed?
  243. */
  244. static void mga_freelist_reset(struct drm_device *dev)
  245. {
  246. struct drm_device_dma *dma = dev->dma;
  247. struct drm_buf *buf;
  248. drm_mga_buf_priv_t *buf_priv;
  249. int i;
  250. for (i = 0; i < dma->buf_count; i++) {
  251. buf = dma->buflist[i];
  252. buf_priv = buf->dev_private;
  253. SET_AGE(&buf_priv->list_entry->age, MGA_BUFFER_FREE, 0);
  254. }
  255. }
  256. #endif
  257. static struct drm_buf *mga_freelist_get(struct drm_device * dev)
  258. {
  259. drm_mga_private_t *dev_priv = dev->dev_private;
  260. drm_mga_freelist_t *next;
  261. drm_mga_freelist_t *prev;
  262. drm_mga_freelist_t *tail = dev_priv->tail;
  263. u32 head, wrap;
  264. DRM_DEBUG("\n");
  265. head = MGA_READ(MGA_PRIMADDRESS);
  266. wrap = dev_priv->sarea_priv->last_wrap;
  267. DRM_DEBUG(" tail=0x%06lx %d\n",
  268. tail->age.head ?
  269. (unsigned long)(tail->age.head - dev_priv->primary->offset) : 0,
  270. tail->age.wrap);
  271. DRM_DEBUG(" head=0x%06lx %d\n",
  272. (unsigned long)(head - dev_priv->primary->offset), wrap);
  273. if (TEST_AGE(&tail->age, head, wrap)) {
  274. prev = dev_priv->tail->prev;
  275. next = dev_priv->tail;
  276. prev->next = NULL;
  277. next->prev = next->next = NULL;
  278. dev_priv->tail = prev;
  279. SET_AGE(&next->age, MGA_BUFFER_USED, 0);
  280. return next->buf;
  281. }
  282. DRM_DEBUG("returning NULL!\n");
  283. return NULL;
  284. }
  285. int mga_freelist_put(struct drm_device *dev, struct drm_buf *buf)
  286. {
  287. drm_mga_private_t *dev_priv = dev->dev_private;
  288. drm_mga_buf_priv_t *buf_priv = buf->dev_private;
  289. drm_mga_freelist_t *head, *entry, *prev;
  290. DRM_DEBUG("age=0x%06lx wrap=%d\n",
  291. (unsigned long)(buf_priv->list_entry->age.head -
  292. dev_priv->primary->offset),
  293. buf_priv->list_entry->age.wrap);
  294. entry = buf_priv->list_entry;
  295. head = dev_priv->head;
  296. if (buf_priv->list_entry->age.head == MGA_BUFFER_USED) {
  297. SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
  298. prev = dev_priv->tail;
  299. prev->next = entry;
  300. entry->prev = prev;
  301. entry->next = NULL;
  302. } else {
  303. prev = head->next;
  304. head->next = entry;
  305. prev->prev = entry;
  306. entry->prev = head;
  307. entry->next = prev;
  308. }
  309. return 0;
  310. }
  311. /* ================================================================
  312. * DMA initialization, cleanup
  313. */
  314. int mga_driver_load(struct drm_device *dev, unsigned long flags)
  315. {
  316. drm_mga_private_t *dev_priv;
  317. int ret;
  318. dev_priv = kzalloc(sizeof(drm_mga_private_t), GFP_KERNEL);
  319. if (!dev_priv)
  320. return -ENOMEM;
  321. dev->dev_private = (void *)dev_priv;
  322. dev_priv->usec_timeout = MGA_DEFAULT_USEC_TIMEOUT;
  323. dev_priv->chipset = flags;
  324. pci_set_master(dev->pdev);
  325. dev_priv->mmio_base = pci_resource_start(dev->pdev, 1);
  326. dev_priv->mmio_size = pci_resource_len(dev->pdev, 1);
  327. ret = drm_vblank_init(dev, 1);
  328. if (ret) {
  329. (void) mga_driver_unload(dev);
  330. return ret;
  331. }
  332. return 0;
  333. }
  334. #if IS_ENABLED(CONFIG_AGP)
  335. /**
  336. * Bootstrap the driver for AGP DMA.
  337. *
  338. * \todo
  339. * Investigate whether there is any benefit to storing the WARP microcode in
  340. * AGP memory. If not, the microcode may as well always be put in PCI
  341. * memory.
  342. *
  343. * \todo
  344. * This routine needs to set dma_bs->agp_mode to the mode actually configured
  345. * in the hardware. Looking just at the Linux AGP driver code, I don't see
  346. * an easy way to determine this.
  347. *
  348. * \sa mga_do_dma_bootstrap, mga_do_pci_dma_bootstrap
  349. */
  350. static int mga_do_agp_dma_bootstrap(struct drm_device *dev,
  351. drm_mga_dma_bootstrap_t *dma_bs)
  352. {
  353. drm_mga_private_t *const dev_priv =
  354. (drm_mga_private_t *) dev->dev_private;
  355. unsigned int warp_size = MGA_WARP_UCODE_SIZE;
  356. int err;
  357. unsigned offset;
  358. const unsigned secondary_size = dma_bs->secondary_bin_count
  359. * dma_bs->secondary_bin_size;
  360. const unsigned agp_size = (dma_bs->agp_size << 20);
  361. struct drm_buf_desc req;
  362. struct drm_agp_mode mode;
  363. struct drm_agp_info info;
  364. struct drm_agp_buffer agp_req;
  365. struct drm_agp_binding bind_req;
  366. /* Acquire AGP. */
  367. err = drm_agp_acquire(dev);
  368. if (err) {
  369. DRM_ERROR("Unable to acquire AGP: %d\n", err);
  370. return err;
  371. }
  372. err = drm_agp_info(dev, &info);
  373. if (err) {
  374. DRM_ERROR("Unable to get AGP info: %d\n", err);
  375. return err;
  376. }
  377. mode.mode = (info.mode & ~0x07) | dma_bs->agp_mode;
  378. err = drm_agp_enable(dev, mode);
  379. if (err) {
  380. DRM_ERROR("Unable to enable AGP (mode = 0x%lx)\n", mode.mode);
  381. return err;
  382. }
  383. /* In addition to the usual AGP mode configuration, the G200 AGP cards
  384. * need to have the AGP mode "manually" set.
  385. */
  386. if (dev_priv->chipset == MGA_CARD_TYPE_G200) {
  387. if (mode.mode & 0x02)
  388. MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_ENABLE);
  389. else
  390. MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_DISABLE);
  391. }
  392. /* Allocate and bind AGP memory. */
  393. agp_req.size = agp_size;
  394. agp_req.type = 0;
  395. err = drm_agp_alloc(dev, &agp_req);
  396. if (err) {
  397. dev_priv->agp_size = 0;
  398. DRM_ERROR("Unable to allocate %uMB AGP memory\n",
  399. dma_bs->agp_size);
  400. return err;
  401. }
  402. dev_priv->agp_size = agp_size;
  403. dev_priv->agp_handle = agp_req.handle;
  404. bind_req.handle = agp_req.handle;
  405. bind_req.offset = 0;
  406. err = drm_agp_bind(dev, &bind_req);
  407. if (err) {
  408. DRM_ERROR("Unable to bind AGP memory: %d\n", err);
  409. return err;
  410. }
  411. /* Make drm_legacy_addbufs happy by not trying to create a mapping for
  412. * less than a page.
  413. */
  414. if (warp_size < PAGE_SIZE)
  415. warp_size = PAGE_SIZE;
  416. offset = 0;
  417. err = drm_legacy_addmap(dev, offset, warp_size,
  418. _DRM_AGP, _DRM_READ_ONLY, &dev_priv->warp);
  419. if (err) {
  420. DRM_ERROR("Unable to map WARP microcode: %d\n", err);
  421. return err;
  422. }
  423. offset += warp_size;
  424. err = drm_legacy_addmap(dev, offset, dma_bs->primary_size,
  425. _DRM_AGP, _DRM_READ_ONLY, &dev_priv->primary);
  426. if (err) {
  427. DRM_ERROR("Unable to map primary DMA region: %d\n", err);
  428. return err;
  429. }
  430. offset += dma_bs->primary_size;
  431. err = drm_legacy_addmap(dev, offset, secondary_size,
  432. _DRM_AGP, 0, &dev->agp_buffer_map);
  433. if (err) {
  434. DRM_ERROR("Unable to map secondary DMA region: %d\n", err);
  435. return err;
  436. }
  437. (void)memset(&req, 0, sizeof(req));
  438. req.count = dma_bs->secondary_bin_count;
  439. req.size = dma_bs->secondary_bin_size;
  440. req.flags = _DRM_AGP_BUFFER;
  441. req.agp_start = offset;
  442. err = drm_legacy_addbufs_agp(dev, &req);
  443. if (err) {
  444. DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
  445. return err;
  446. }
  447. {
  448. struct drm_map_list *_entry;
  449. unsigned long agp_token = 0;
  450. list_for_each_entry(_entry, &dev->maplist, head) {
  451. if (_entry->map == dev->agp_buffer_map)
  452. agp_token = _entry->user_token;
  453. }
  454. if (!agp_token)
  455. return -EFAULT;
  456. dev->agp_buffer_token = agp_token;
  457. }
  458. offset += secondary_size;
  459. err = drm_legacy_addmap(dev, offset, agp_size - offset,
  460. _DRM_AGP, 0, &dev_priv->agp_textures);
  461. if (err) {
  462. DRM_ERROR("Unable to map AGP texture region %d\n", err);
  463. return err;
  464. }
  465. drm_legacy_ioremap(dev_priv->warp, dev);
  466. drm_legacy_ioremap(dev_priv->primary, dev);
  467. drm_legacy_ioremap(dev->agp_buffer_map, dev);
  468. if (!dev_priv->warp->handle ||
  469. !dev_priv->primary->handle || !dev->agp_buffer_map->handle) {
  470. DRM_ERROR("failed to ioremap agp regions! (%p, %p, %p)\n",
  471. dev_priv->warp->handle, dev_priv->primary->handle,
  472. dev->agp_buffer_map->handle);
  473. return -ENOMEM;
  474. }
  475. dev_priv->dma_access = MGA_PAGPXFER;
  476. dev_priv->wagp_enable = MGA_WAGP_ENABLE;
  477. DRM_INFO("Initialized card for AGP DMA.\n");
  478. return 0;
  479. }
  480. #else
  481. static int mga_do_agp_dma_bootstrap(struct drm_device *dev,
  482. drm_mga_dma_bootstrap_t *dma_bs)
  483. {
  484. return -EINVAL;
  485. }
  486. #endif
  487. /**
  488. * Bootstrap the driver for PCI DMA.
  489. *
  490. * \todo
  491. * The algorithm for decreasing the size of the primary DMA buffer could be
  492. * better. The size should be rounded up to the nearest page size, then
  493. * decrease the request size by a single page each pass through the loop.
  494. *
  495. * \todo
  496. * Determine whether the maximum address passed to drm_pci_alloc is correct.
  497. * The same goes for drm_legacy_addbufs_pci.
  498. *
  499. * \sa mga_do_dma_bootstrap, mga_do_agp_dma_bootstrap
  500. */
  501. static int mga_do_pci_dma_bootstrap(struct drm_device *dev,
  502. drm_mga_dma_bootstrap_t *dma_bs)
  503. {
  504. drm_mga_private_t *const dev_priv =
  505. (drm_mga_private_t *) dev->dev_private;
  506. unsigned int warp_size = MGA_WARP_UCODE_SIZE;
  507. unsigned int primary_size;
  508. unsigned int bin_count;
  509. int err;
  510. struct drm_buf_desc req;
  511. if (dev->dma == NULL) {
  512. DRM_ERROR("dev->dma is NULL\n");
  513. return -EFAULT;
  514. }
  515. /* Make drm_legacy_addbufs happy by not trying to create a mapping for
  516. * less than a page.
  517. */
  518. if (warp_size < PAGE_SIZE)
  519. warp_size = PAGE_SIZE;
  520. /* The proper alignment is 0x100 for this mapping */
  521. err = drm_legacy_addmap(dev, 0, warp_size, _DRM_CONSISTENT,
  522. _DRM_READ_ONLY, &dev_priv->warp);
  523. if (err != 0) {
  524. DRM_ERROR("Unable to create mapping for WARP microcode: %d\n",
  525. err);
  526. return err;
  527. }
  528. /* Other than the bottom two bits being used to encode other
  529. * information, there don't appear to be any restrictions on the
  530. * alignment of the primary or secondary DMA buffers.
  531. */
  532. for (primary_size = dma_bs->primary_size; primary_size != 0;
  533. primary_size >>= 1) {
  534. /* The proper alignment for this mapping is 0x04 */
  535. err = drm_legacy_addmap(dev, 0, primary_size, _DRM_CONSISTENT,
  536. _DRM_READ_ONLY, &dev_priv->primary);
  537. if (!err)
  538. break;
  539. }
  540. if (err != 0) {
  541. DRM_ERROR("Unable to allocate primary DMA region: %d\n", err);
  542. return -ENOMEM;
  543. }
  544. if (dev_priv->primary->size != dma_bs->primary_size) {
  545. DRM_INFO("Primary DMA buffer size reduced from %u to %u.\n",
  546. dma_bs->primary_size,
  547. (unsigned)dev_priv->primary->size);
  548. dma_bs->primary_size = dev_priv->primary->size;
  549. }
  550. for (bin_count = dma_bs->secondary_bin_count; bin_count > 0;
  551. bin_count--) {
  552. (void)memset(&req, 0, sizeof(req));
  553. req.count = bin_count;
  554. req.size = dma_bs->secondary_bin_size;
  555. err = drm_legacy_addbufs_pci(dev, &req);
  556. if (!err)
  557. break;
  558. }
  559. if (bin_count == 0) {
  560. DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
  561. return err;
  562. }
  563. if (bin_count != dma_bs->secondary_bin_count) {
  564. DRM_INFO("Secondary PCI DMA buffer bin count reduced from %u "
  565. "to %u.\n", dma_bs->secondary_bin_count, bin_count);
  566. dma_bs->secondary_bin_count = bin_count;
  567. }
  568. dev_priv->dma_access = 0;
  569. dev_priv->wagp_enable = 0;
  570. dma_bs->agp_mode = 0;
  571. DRM_INFO("Initialized card for PCI DMA.\n");
  572. return 0;
  573. }
  574. static int mga_do_dma_bootstrap(struct drm_device *dev,
  575. drm_mga_dma_bootstrap_t *dma_bs)
  576. {
  577. const int is_agp = (dma_bs->agp_mode != 0) && drm_pci_device_is_agp(dev);
  578. int err;
  579. drm_mga_private_t *const dev_priv =
  580. (drm_mga_private_t *) dev->dev_private;
  581. dev_priv->used_new_dma_init = 1;
  582. /* The first steps are the same for both PCI and AGP based DMA. Map
  583. * the cards MMIO registers and map a status page.
  584. */
  585. err = drm_legacy_addmap(dev, dev_priv->mmio_base, dev_priv->mmio_size,
  586. _DRM_REGISTERS, _DRM_READ_ONLY,
  587. &dev_priv->mmio);
  588. if (err) {
  589. DRM_ERROR("Unable to map MMIO region: %d\n", err);
  590. return err;
  591. }
  592. err = drm_legacy_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
  593. _DRM_READ_ONLY | _DRM_LOCKED | _DRM_KERNEL,
  594. &dev_priv->status);
  595. if (err) {
  596. DRM_ERROR("Unable to map status region: %d\n", err);
  597. return err;
  598. }
  599. /* The DMA initialization procedure is slightly different for PCI and
  600. * AGP cards. AGP cards just allocate a large block of AGP memory and
  601. * carve off portions of it for internal uses. The remaining memory
  602. * is returned to user-mode to be used for AGP textures.
  603. */
  604. if (is_agp)
  605. err = mga_do_agp_dma_bootstrap(dev, dma_bs);
  606. /* If we attempted to initialize the card for AGP DMA but failed,
  607. * clean-up any mess that may have been created.
  608. */
  609. if (err)
  610. mga_do_cleanup_dma(dev, MINIMAL_CLEANUP);
  611. /* Not only do we want to try and initialized PCI cards for PCI DMA,
  612. * but we also try to initialized AGP cards that could not be
  613. * initialized for AGP DMA. This covers the case where we have an AGP
  614. * card in a system with an unsupported AGP chipset. In that case the
  615. * card will be detected as AGP, but we won't be able to allocate any
  616. * AGP memory, etc.
  617. */
  618. if (!is_agp || err)
  619. err = mga_do_pci_dma_bootstrap(dev, dma_bs);
  620. return err;
  621. }
  622. int mga_dma_bootstrap(struct drm_device *dev, void *data,
  623. struct drm_file *file_priv)
  624. {
  625. drm_mga_dma_bootstrap_t *bootstrap = data;
  626. int err;
  627. static const int modes[] = { 0, 1, 2, 2, 4, 4, 4, 4 };
  628. const drm_mga_private_t *const dev_priv =
  629. (drm_mga_private_t *) dev->dev_private;
  630. err = mga_do_dma_bootstrap(dev, bootstrap);
  631. if (err) {
  632. mga_do_cleanup_dma(dev, FULL_CLEANUP);
  633. return err;
  634. }
  635. if (dev_priv->agp_textures != NULL) {
  636. bootstrap->texture_handle = dev_priv->agp_textures->offset;
  637. bootstrap->texture_size = dev_priv->agp_textures->size;
  638. } else {
  639. bootstrap->texture_handle = 0;
  640. bootstrap->texture_size = 0;
  641. }
  642. bootstrap->agp_mode = modes[bootstrap->agp_mode & 0x07];
  643. return err;
  644. }
  645. static int mga_do_init_dma(struct drm_device *dev, drm_mga_init_t *init)
  646. {
  647. drm_mga_private_t *dev_priv;
  648. int ret;
  649. DRM_DEBUG("\n");
  650. dev_priv = dev->dev_private;
  651. if (init->sgram)
  652. dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_BLK;
  653. else
  654. dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_RSTR;
  655. dev_priv->maccess = init->maccess;
  656. dev_priv->fb_cpp = init->fb_cpp;
  657. dev_priv->front_offset = init->front_offset;
  658. dev_priv->front_pitch = init->front_pitch;
  659. dev_priv->back_offset = init->back_offset;
  660. dev_priv->back_pitch = init->back_pitch;
  661. dev_priv->depth_cpp = init->depth_cpp;
  662. dev_priv->depth_offset = init->depth_offset;
  663. dev_priv->depth_pitch = init->depth_pitch;
  664. /* FIXME: Need to support AGP textures...
  665. */
  666. dev_priv->texture_offset = init->texture_offset[0];
  667. dev_priv->texture_size = init->texture_size[0];
  668. dev_priv->sarea = drm_legacy_getsarea(dev);
  669. if (!dev_priv->sarea) {
  670. DRM_ERROR("failed to find sarea!\n");
  671. return -EINVAL;
  672. }
  673. if (!dev_priv->used_new_dma_init) {
  674. dev_priv->dma_access = MGA_PAGPXFER;
  675. dev_priv->wagp_enable = MGA_WAGP_ENABLE;
  676. dev_priv->status = drm_legacy_findmap(dev, init->status_offset);
  677. if (!dev_priv->status) {
  678. DRM_ERROR("failed to find status page!\n");
  679. return -EINVAL;
  680. }
  681. dev_priv->mmio = drm_legacy_findmap(dev, init->mmio_offset);
  682. if (!dev_priv->mmio) {
  683. DRM_ERROR("failed to find mmio region!\n");
  684. return -EINVAL;
  685. }
  686. dev_priv->warp = drm_legacy_findmap(dev, init->warp_offset);
  687. if (!dev_priv->warp) {
  688. DRM_ERROR("failed to find warp microcode region!\n");
  689. return -EINVAL;
  690. }
  691. dev_priv->primary = drm_legacy_findmap(dev, init->primary_offset);
  692. if (!dev_priv->primary) {
  693. DRM_ERROR("failed to find primary dma region!\n");
  694. return -EINVAL;
  695. }
  696. dev->agp_buffer_token = init->buffers_offset;
  697. dev->agp_buffer_map =
  698. drm_legacy_findmap(dev, init->buffers_offset);
  699. if (!dev->agp_buffer_map) {
  700. DRM_ERROR("failed to find dma buffer region!\n");
  701. return -EINVAL;
  702. }
  703. drm_legacy_ioremap(dev_priv->warp, dev);
  704. drm_legacy_ioremap(dev_priv->primary, dev);
  705. drm_legacy_ioremap(dev->agp_buffer_map, dev);
  706. }
  707. dev_priv->sarea_priv =
  708. (drm_mga_sarea_t *) ((u8 *) dev_priv->sarea->handle +
  709. init->sarea_priv_offset);
  710. if (!dev_priv->warp->handle ||
  711. !dev_priv->primary->handle ||
  712. ((dev_priv->dma_access != 0) &&
  713. ((dev->agp_buffer_map == NULL) ||
  714. (dev->agp_buffer_map->handle == NULL)))) {
  715. DRM_ERROR("failed to ioremap agp regions!\n");
  716. return -ENOMEM;
  717. }
  718. ret = mga_warp_install_microcode(dev_priv);
  719. if (ret < 0) {
  720. DRM_ERROR("failed to install WARP ucode!: %d\n", ret);
  721. return ret;
  722. }
  723. ret = mga_warp_init(dev_priv);
  724. if (ret < 0) {
  725. DRM_ERROR("failed to init WARP engine!: %d\n", ret);
  726. return ret;
  727. }
  728. dev_priv->prim.status = (u32 *) dev_priv->status->handle;
  729. mga_do_wait_for_idle(dev_priv);
  730. /* Init the primary DMA registers.
  731. */
  732. MGA_WRITE(MGA_PRIMADDRESS, dev_priv->primary->offset | MGA_DMA_GENERAL);
  733. #if 0
  734. MGA_WRITE(MGA_PRIMPTR, virt_to_bus((void *)dev_priv->prim.status) | MGA_PRIMPTREN0 | /* Soft trap, SECEND, SETUPEND */
  735. MGA_PRIMPTREN1); /* DWGSYNC */
  736. #endif
  737. dev_priv->prim.start = (u8 *) dev_priv->primary->handle;
  738. dev_priv->prim.end = ((u8 *) dev_priv->primary->handle
  739. + dev_priv->primary->size);
  740. dev_priv->prim.size = dev_priv->primary->size;
  741. dev_priv->prim.tail = 0;
  742. dev_priv->prim.space = dev_priv->prim.size;
  743. dev_priv->prim.wrapped = 0;
  744. dev_priv->prim.last_flush = 0;
  745. dev_priv->prim.last_wrap = 0;
  746. dev_priv->prim.high_mark = 256 * DMA_BLOCK_SIZE;
  747. dev_priv->prim.status[0] = dev_priv->primary->offset;
  748. dev_priv->prim.status[1] = 0;
  749. dev_priv->sarea_priv->last_wrap = 0;
  750. dev_priv->sarea_priv->last_frame.head = 0;
  751. dev_priv->sarea_priv->last_frame.wrap = 0;
  752. if (mga_freelist_init(dev, dev_priv) < 0) {
  753. DRM_ERROR("could not initialize freelist\n");
  754. return -ENOMEM;
  755. }
  756. return 0;
  757. }
  758. static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup)
  759. {
  760. int err = 0;
  761. DRM_DEBUG("\n");
  762. /* Make sure interrupts are disabled here because the uninstall ioctl
  763. * may not have been called from userspace and after dev_private
  764. * is freed, it's too late.
  765. */
  766. if (dev->irq_enabled)
  767. drm_irq_uninstall(dev);
  768. if (dev->dev_private) {
  769. drm_mga_private_t *dev_priv = dev->dev_private;
  770. if ((dev_priv->warp != NULL)
  771. && (dev_priv->warp->type != _DRM_CONSISTENT))
  772. drm_legacy_ioremapfree(dev_priv->warp, dev);
  773. if ((dev_priv->primary != NULL)
  774. && (dev_priv->primary->type != _DRM_CONSISTENT))
  775. drm_legacy_ioremapfree(dev_priv->primary, dev);
  776. if (dev->agp_buffer_map != NULL)
  777. drm_legacy_ioremapfree(dev->agp_buffer_map, dev);
  778. if (dev_priv->used_new_dma_init) {
  779. #if IS_ENABLED(CONFIG_AGP)
  780. if (dev_priv->agp_handle != 0) {
  781. struct drm_agp_binding unbind_req;
  782. struct drm_agp_buffer free_req;
  783. unbind_req.handle = dev_priv->agp_handle;
  784. drm_agp_unbind(dev, &unbind_req);
  785. free_req.handle = dev_priv->agp_handle;
  786. drm_agp_free(dev, &free_req);
  787. dev_priv->agp_textures = NULL;
  788. dev_priv->agp_size = 0;
  789. dev_priv->agp_handle = 0;
  790. }
  791. if ((dev->agp != NULL) && dev->agp->acquired)
  792. err = drm_agp_release(dev);
  793. #endif
  794. }
  795. dev_priv->warp = NULL;
  796. dev_priv->primary = NULL;
  797. dev_priv->sarea = NULL;
  798. dev_priv->sarea_priv = NULL;
  799. dev->agp_buffer_map = NULL;
  800. if (full_cleanup) {
  801. dev_priv->mmio = NULL;
  802. dev_priv->status = NULL;
  803. dev_priv->used_new_dma_init = 0;
  804. }
  805. memset(&dev_priv->prim, 0, sizeof(dev_priv->prim));
  806. dev_priv->warp_pipe = 0;
  807. memset(dev_priv->warp_pipe_phys, 0,
  808. sizeof(dev_priv->warp_pipe_phys));
  809. if (dev_priv->head != NULL)
  810. mga_freelist_cleanup(dev);
  811. }
  812. return err;
  813. }
  814. int mga_dma_init(struct drm_device *dev, void *data,
  815. struct drm_file *file_priv)
  816. {
  817. drm_mga_init_t *init = data;
  818. int err;
  819. LOCK_TEST_WITH_RETURN(dev, file_priv);
  820. switch (init->func) {
  821. case MGA_INIT_DMA:
  822. err = mga_do_init_dma(dev, init);
  823. if (err)
  824. (void)mga_do_cleanup_dma(dev, FULL_CLEANUP);
  825. return err;
  826. case MGA_CLEANUP_DMA:
  827. return mga_do_cleanup_dma(dev, FULL_CLEANUP);
  828. }
  829. return -EINVAL;
  830. }
  831. /* ================================================================
  832. * Primary DMA stream management
  833. */
  834. int mga_dma_flush(struct drm_device *dev, void *data,
  835. struct drm_file *file_priv)
  836. {
  837. drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
  838. struct drm_lock *lock = data;
  839. LOCK_TEST_WITH_RETURN(dev, file_priv);
  840. DRM_DEBUG("%s%s%s\n",
  841. (lock->flags & _DRM_LOCK_FLUSH) ? "flush, " : "",
  842. (lock->flags & _DRM_LOCK_FLUSH_ALL) ? "flush all, " : "",
  843. (lock->flags & _DRM_LOCK_QUIESCENT) ? "idle, " : "");
  844. WRAP_WAIT_WITH_RETURN(dev_priv);
  845. if (lock->flags & (_DRM_LOCK_FLUSH | _DRM_LOCK_FLUSH_ALL))
  846. mga_do_dma_flush(dev_priv);
  847. if (lock->flags & _DRM_LOCK_QUIESCENT) {
  848. #if MGA_DMA_DEBUG
  849. int ret = mga_do_wait_for_idle(dev_priv);
  850. if (ret < 0)
  851. DRM_INFO("-EBUSY\n");
  852. return ret;
  853. #else
  854. return mga_do_wait_for_idle(dev_priv);
  855. #endif
  856. } else {
  857. return 0;
  858. }
  859. }
  860. int mga_dma_reset(struct drm_device *dev, void *data,
  861. struct drm_file *file_priv)
  862. {
  863. drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
  864. LOCK_TEST_WITH_RETURN(dev, file_priv);
  865. return mga_do_dma_reset(dev_priv);
  866. }
  867. /* ================================================================
  868. * DMA buffer management
  869. */
  870. static int mga_dma_get_buffers(struct drm_device *dev,
  871. struct drm_file *file_priv, struct drm_dma *d)
  872. {
  873. struct drm_buf *buf;
  874. int i;
  875. for (i = d->granted_count; i < d->request_count; i++) {
  876. buf = mga_freelist_get(dev);
  877. if (!buf)
  878. return -EAGAIN;
  879. buf->file_priv = file_priv;
  880. if (copy_to_user(&d->request_indices[i],
  881. &buf->idx, sizeof(buf->idx)))
  882. return -EFAULT;
  883. if (copy_to_user(&d->request_sizes[i],
  884. &buf->total, sizeof(buf->total)))
  885. return -EFAULT;
  886. d->granted_count++;
  887. }
  888. return 0;
  889. }
  890. int mga_dma_buffers(struct drm_device *dev, void *data,
  891. struct drm_file *file_priv)
  892. {
  893. struct drm_device_dma *dma = dev->dma;
  894. drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
  895. struct drm_dma *d = data;
  896. int ret = 0;
  897. LOCK_TEST_WITH_RETURN(dev, file_priv);
  898. /* Please don't send us buffers.
  899. */
  900. if (d->send_count != 0) {
  901. DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
  902. DRM_CURRENTPID, d->send_count);
  903. return -EINVAL;
  904. }
  905. /* We'll send you buffers.
  906. */
  907. if (d->request_count < 0 || d->request_count > dma->buf_count) {
  908. DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
  909. DRM_CURRENTPID, d->request_count, dma->buf_count);
  910. return -EINVAL;
  911. }
  912. WRAP_TEST_WITH_RETURN(dev_priv);
  913. d->granted_count = 0;
  914. if (d->request_count)
  915. ret = mga_dma_get_buffers(dev, file_priv, d);
  916. return ret;
  917. }
  918. /**
  919. * Called just before the module is unloaded.
  920. */
  921. int mga_driver_unload(struct drm_device *dev)
  922. {
  923. kfree(dev->dev_private);
  924. dev->dev_private = NULL;
  925. return 0;
  926. }
  927. /**
  928. * Called when the last opener of the device is closed.
  929. */
  930. void mga_driver_lastclose(struct drm_device *dev)
  931. {
  932. mga_do_cleanup_dma(dev, FULL_CLEANUP);
  933. }
  934. int mga_driver_dma_quiescent(struct drm_device *dev)
  935. {
  936. drm_mga_private_t *dev_priv = dev->dev_private;
  937. return mga_do_wait_for_idle(dev_priv);
  938. }