mga_dma.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. /* There are PCI versions of the G450. These cards have the
  319. * same PCI ID as the AGP G450, but have an additional PCI-to-PCI
  320. * bridge chip. We detect these cards, which are not currently
  321. * supported by this driver, by looking at the device ID of the
  322. * bus the "card" is on. If vendor is 0x3388 (Hint Corp) and the
  323. * device is 0x0021 (HB6 Universal PCI-PCI bridge), we reject the
  324. * device.
  325. */
  326. if ((dev->pdev->device == 0x0525) && dev->pdev->bus->self
  327. && (dev->pdev->bus->self->vendor == 0x3388)
  328. && (dev->pdev->bus->self->device == 0x0021)
  329. && dev->agp) {
  330. /* FIXME: This should be quirked in the pci core, but oh well
  331. * the hw probably stopped existing. */
  332. arch_phys_wc_del(dev->agp->agp_mtrr);
  333. kfree(dev->agp);
  334. dev->agp = NULL;
  335. }
  336. dev_priv = kzalloc(sizeof(drm_mga_private_t), GFP_KERNEL);
  337. if (!dev_priv)
  338. return -ENOMEM;
  339. dev->dev_private = (void *)dev_priv;
  340. dev_priv->usec_timeout = MGA_DEFAULT_USEC_TIMEOUT;
  341. dev_priv->chipset = flags;
  342. pci_set_master(dev->pdev);
  343. dev_priv->mmio_base = pci_resource_start(dev->pdev, 1);
  344. dev_priv->mmio_size = pci_resource_len(dev->pdev, 1);
  345. ret = drm_vblank_init(dev, 1);
  346. if (ret) {
  347. (void) mga_driver_unload(dev);
  348. return ret;
  349. }
  350. return 0;
  351. }
  352. #if IS_ENABLED(CONFIG_AGP)
  353. /**
  354. * Bootstrap the driver for AGP DMA.
  355. *
  356. * \todo
  357. * Investigate whether there is any benefit to storing the WARP microcode in
  358. * AGP memory. If not, the microcode may as well always be put in PCI
  359. * memory.
  360. *
  361. * \todo
  362. * This routine needs to set dma_bs->agp_mode to the mode actually configured
  363. * in the hardware. Looking just at the Linux AGP driver code, I don't see
  364. * an easy way to determine this.
  365. *
  366. * \sa mga_do_dma_bootstrap, mga_do_pci_dma_bootstrap
  367. */
  368. static int mga_do_agp_dma_bootstrap(struct drm_device *dev,
  369. drm_mga_dma_bootstrap_t *dma_bs)
  370. {
  371. drm_mga_private_t *const dev_priv =
  372. (drm_mga_private_t *) dev->dev_private;
  373. unsigned int warp_size = MGA_WARP_UCODE_SIZE;
  374. int err;
  375. unsigned offset;
  376. const unsigned secondary_size = dma_bs->secondary_bin_count
  377. * dma_bs->secondary_bin_size;
  378. const unsigned agp_size = (dma_bs->agp_size << 20);
  379. struct drm_buf_desc req;
  380. struct drm_agp_mode mode;
  381. struct drm_agp_info info;
  382. struct drm_agp_buffer agp_req;
  383. struct drm_agp_binding bind_req;
  384. /* Acquire AGP. */
  385. err = drm_agp_acquire(dev);
  386. if (err) {
  387. DRM_ERROR("Unable to acquire AGP: %d\n", err);
  388. return err;
  389. }
  390. err = drm_agp_info(dev, &info);
  391. if (err) {
  392. DRM_ERROR("Unable to get AGP info: %d\n", err);
  393. return err;
  394. }
  395. mode.mode = (info.mode & ~0x07) | dma_bs->agp_mode;
  396. err = drm_agp_enable(dev, mode);
  397. if (err) {
  398. DRM_ERROR("Unable to enable AGP (mode = 0x%lx)\n", mode.mode);
  399. return err;
  400. }
  401. /* In addition to the usual AGP mode configuration, the G200 AGP cards
  402. * need to have the AGP mode "manually" set.
  403. */
  404. if (dev_priv->chipset == MGA_CARD_TYPE_G200) {
  405. if (mode.mode & 0x02)
  406. MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_ENABLE);
  407. else
  408. MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_DISABLE);
  409. }
  410. /* Allocate and bind AGP memory. */
  411. agp_req.size = agp_size;
  412. agp_req.type = 0;
  413. err = drm_agp_alloc(dev, &agp_req);
  414. if (err) {
  415. dev_priv->agp_size = 0;
  416. DRM_ERROR("Unable to allocate %uMB AGP memory\n",
  417. dma_bs->agp_size);
  418. return err;
  419. }
  420. dev_priv->agp_size = agp_size;
  421. dev_priv->agp_handle = agp_req.handle;
  422. bind_req.handle = agp_req.handle;
  423. bind_req.offset = 0;
  424. err = drm_agp_bind(dev, &bind_req);
  425. if (err) {
  426. DRM_ERROR("Unable to bind AGP memory: %d\n", err);
  427. return err;
  428. }
  429. /* Make drm_legacy_addbufs happy by not trying to create a mapping for
  430. * less than a page.
  431. */
  432. if (warp_size < PAGE_SIZE)
  433. warp_size = PAGE_SIZE;
  434. offset = 0;
  435. err = drm_legacy_addmap(dev, offset, warp_size,
  436. _DRM_AGP, _DRM_READ_ONLY, &dev_priv->warp);
  437. if (err) {
  438. DRM_ERROR("Unable to map WARP microcode: %d\n", err);
  439. return err;
  440. }
  441. offset += warp_size;
  442. err = drm_legacy_addmap(dev, offset, dma_bs->primary_size,
  443. _DRM_AGP, _DRM_READ_ONLY, &dev_priv->primary);
  444. if (err) {
  445. DRM_ERROR("Unable to map primary DMA region: %d\n", err);
  446. return err;
  447. }
  448. offset += dma_bs->primary_size;
  449. err = drm_legacy_addmap(dev, offset, secondary_size,
  450. _DRM_AGP, 0, &dev->agp_buffer_map);
  451. if (err) {
  452. DRM_ERROR("Unable to map secondary DMA region: %d\n", err);
  453. return err;
  454. }
  455. (void)memset(&req, 0, sizeof(req));
  456. req.count = dma_bs->secondary_bin_count;
  457. req.size = dma_bs->secondary_bin_size;
  458. req.flags = _DRM_AGP_BUFFER;
  459. req.agp_start = offset;
  460. err = drm_legacy_addbufs_agp(dev, &req);
  461. if (err) {
  462. DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
  463. return err;
  464. }
  465. {
  466. struct drm_map_list *_entry;
  467. unsigned long agp_token = 0;
  468. list_for_each_entry(_entry, &dev->maplist, head) {
  469. if (_entry->map == dev->agp_buffer_map)
  470. agp_token = _entry->user_token;
  471. }
  472. if (!agp_token)
  473. return -EFAULT;
  474. dev->agp_buffer_token = agp_token;
  475. }
  476. offset += secondary_size;
  477. err = drm_legacy_addmap(dev, offset, agp_size - offset,
  478. _DRM_AGP, 0, &dev_priv->agp_textures);
  479. if (err) {
  480. DRM_ERROR("Unable to map AGP texture region %d\n", err);
  481. return err;
  482. }
  483. drm_legacy_ioremap(dev_priv->warp, dev);
  484. drm_legacy_ioremap(dev_priv->primary, dev);
  485. drm_legacy_ioremap(dev->agp_buffer_map, dev);
  486. if (!dev_priv->warp->handle ||
  487. !dev_priv->primary->handle || !dev->agp_buffer_map->handle) {
  488. DRM_ERROR("failed to ioremap agp regions! (%p, %p, %p)\n",
  489. dev_priv->warp->handle, dev_priv->primary->handle,
  490. dev->agp_buffer_map->handle);
  491. return -ENOMEM;
  492. }
  493. dev_priv->dma_access = MGA_PAGPXFER;
  494. dev_priv->wagp_enable = MGA_WAGP_ENABLE;
  495. DRM_INFO("Initialized card for AGP DMA.\n");
  496. return 0;
  497. }
  498. #else
  499. static int mga_do_agp_dma_bootstrap(struct drm_device *dev,
  500. drm_mga_dma_bootstrap_t *dma_bs)
  501. {
  502. return -EINVAL;
  503. }
  504. #endif
  505. /**
  506. * Bootstrap the driver for PCI DMA.
  507. *
  508. * \todo
  509. * The algorithm for decreasing the size of the primary DMA buffer could be
  510. * better. The size should be rounded up to the nearest page size, then
  511. * decrease the request size by a single page each pass through the loop.
  512. *
  513. * \todo
  514. * Determine whether the maximum address passed to drm_pci_alloc is correct.
  515. * The same goes for drm_legacy_addbufs_pci.
  516. *
  517. * \sa mga_do_dma_bootstrap, mga_do_agp_dma_bootstrap
  518. */
  519. static int mga_do_pci_dma_bootstrap(struct drm_device *dev,
  520. drm_mga_dma_bootstrap_t *dma_bs)
  521. {
  522. drm_mga_private_t *const dev_priv =
  523. (drm_mga_private_t *) dev->dev_private;
  524. unsigned int warp_size = MGA_WARP_UCODE_SIZE;
  525. unsigned int primary_size;
  526. unsigned int bin_count;
  527. int err;
  528. struct drm_buf_desc req;
  529. if (dev->dma == NULL) {
  530. DRM_ERROR("dev->dma is NULL\n");
  531. return -EFAULT;
  532. }
  533. /* Make drm_legacy_addbufs happy by not trying to create a mapping for
  534. * less than a page.
  535. */
  536. if (warp_size < PAGE_SIZE)
  537. warp_size = PAGE_SIZE;
  538. /* The proper alignment is 0x100 for this mapping */
  539. err = drm_legacy_addmap(dev, 0, warp_size, _DRM_CONSISTENT,
  540. _DRM_READ_ONLY, &dev_priv->warp);
  541. if (err != 0) {
  542. DRM_ERROR("Unable to create mapping for WARP microcode: %d\n",
  543. err);
  544. return err;
  545. }
  546. /* Other than the bottom two bits being used to encode other
  547. * information, there don't appear to be any restrictions on the
  548. * alignment of the primary or secondary DMA buffers.
  549. */
  550. for (primary_size = dma_bs->primary_size; primary_size != 0;
  551. primary_size >>= 1) {
  552. /* The proper alignment for this mapping is 0x04 */
  553. err = drm_legacy_addmap(dev, 0, primary_size, _DRM_CONSISTENT,
  554. _DRM_READ_ONLY, &dev_priv->primary);
  555. if (!err)
  556. break;
  557. }
  558. if (err != 0) {
  559. DRM_ERROR("Unable to allocate primary DMA region: %d\n", err);
  560. return -ENOMEM;
  561. }
  562. if (dev_priv->primary->size != dma_bs->primary_size) {
  563. DRM_INFO("Primary DMA buffer size reduced from %u to %u.\n",
  564. dma_bs->primary_size,
  565. (unsigned)dev_priv->primary->size);
  566. dma_bs->primary_size = dev_priv->primary->size;
  567. }
  568. for (bin_count = dma_bs->secondary_bin_count; bin_count > 0;
  569. bin_count--) {
  570. (void)memset(&req, 0, sizeof(req));
  571. req.count = bin_count;
  572. req.size = dma_bs->secondary_bin_size;
  573. err = drm_legacy_addbufs_pci(dev, &req);
  574. if (!err)
  575. break;
  576. }
  577. if (bin_count == 0) {
  578. DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
  579. return err;
  580. }
  581. if (bin_count != dma_bs->secondary_bin_count) {
  582. DRM_INFO("Secondary PCI DMA buffer bin count reduced from %u "
  583. "to %u.\n", dma_bs->secondary_bin_count, bin_count);
  584. dma_bs->secondary_bin_count = bin_count;
  585. }
  586. dev_priv->dma_access = 0;
  587. dev_priv->wagp_enable = 0;
  588. dma_bs->agp_mode = 0;
  589. DRM_INFO("Initialized card for PCI DMA.\n");
  590. return 0;
  591. }
  592. static int mga_do_dma_bootstrap(struct drm_device *dev,
  593. drm_mga_dma_bootstrap_t *dma_bs)
  594. {
  595. const int is_agp = (dma_bs->agp_mode != 0) && dev->agp;
  596. int err;
  597. drm_mga_private_t *const dev_priv =
  598. (drm_mga_private_t *) dev->dev_private;
  599. dev_priv->used_new_dma_init = 1;
  600. /* The first steps are the same for both PCI and AGP based DMA. Map
  601. * the cards MMIO registers and map a status page.
  602. */
  603. err = drm_legacy_addmap(dev, dev_priv->mmio_base, dev_priv->mmio_size,
  604. _DRM_REGISTERS, _DRM_READ_ONLY,
  605. &dev_priv->mmio);
  606. if (err) {
  607. DRM_ERROR("Unable to map MMIO region: %d\n", err);
  608. return err;
  609. }
  610. err = drm_legacy_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
  611. _DRM_READ_ONLY | _DRM_LOCKED | _DRM_KERNEL,
  612. &dev_priv->status);
  613. if (err) {
  614. DRM_ERROR("Unable to map status region: %d\n", err);
  615. return err;
  616. }
  617. /* The DMA initialization procedure is slightly different for PCI and
  618. * AGP cards. AGP cards just allocate a large block of AGP memory and
  619. * carve off portions of it for internal uses. The remaining memory
  620. * is returned to user-mode to be used for AGP textures.
  621. */
  622. if (is_agp)
  623. err = mga_do_agp_dma_bootstrap(dev, dma_bs);
  624. /* If we attempted to initialize the card for AGP DMA but failed,
  625. * clean-up any mess that may have been created.
  626. */
  627. if (err)
  628. mga_do_cleanup_dma(dev, MINIMAL_CLEANUP);
  629. /* Not only do we want to try and initialized PCI cards for PCI DMA,
  630. * but we also try to initialized AGP cards that could not be
  631. * initialized for AGP DMA. This covers the case where we have an AGP
  632. * card in a system with an unsupported AGP chipset. In that case the
  633. * card will be detected as AGP, but we won't be able to allocate any
  634. * AGP memory, etc.
  635. */
  636. if (!is_agp || err)
  637. err = mga_do_pci_dma_bootstrap(dev, dma_bs);
  638. return err;
  639. }
  640. int mga_dma_bootstrap(struct drm_device *dev, void *data,
  641. struct drm_file *file_priv)
  642. {
  643. drm_mga_dma_bootstrap_t *bootstrap = data;
  644. int err;
  645. static const int modes[] = { 0, 1, 2, 2, 4, 4, 4, 4 };
  646. const drm_mga_private_t *const dev_priv =
  647. (drm_mga_private_t *) dev->dev_private;
  648. err = mga_do_dma_bootstrap(dev, bootstrap);
  649. if (err) {
  650. mga_do_cleanup_dma(dev, FULL_CLEANUP);
  651. return err;
  652. }
  653. if (dev_priv->agp_textures != NULL) {
  654. bootstrap->texture_handle = dev_priv->agp_textures->offset;
  655. bootstrap->texture_size = dev_priv->agp_textures->size;
  656. } else {
  657. bootstrap->texture_handle = 0;
  658. bootstrap->texture_size = 0;
  659. }
  660. bootstrap->agp_mode = modes[bootstrap->agp_mode & 0x07];
  661. return err;
  662. }
  663. static int mga_do_init_dma(struct drm_device *dev, drm_mga_init_t *init)
  664. {
  665. drm_mga_private_t *dev_priv;
  666. int ret;
  667. DRM_DEBUG("\n");
  668. dev_priv = dev->dev_private;
  669. if (init->sgram)
  670. dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_BLK;
  671. else
  672. dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_RSTR;
  673. dev_priv->maccess = init->maccess;
  674. dev_priv->fb_cpp = init->fb_cpp;
  675. dev_priv->front_offset = init->front_offset;
  676. dev_priv->front_pitch = init->front_pitch;
  677. dev_priv->back_offset = init->back_offset;
  678. dev_priv->back_pitch = init->back_pitch;
  679. dev_priv->depth_cpp = init->depth_cpp;
  680. dev_priv->depth_offset = init->depth_offset;
  681. dev_priv->depth_pitch = init->depth_pitch;
  682. /* FIXME: Need to support AGP textures...
  683. */
  684. dev_priv->texture_offset = init->texture_offset[0];
  685. dev_priv->texture_size = init->texture_size[0];
  686. dev_priv->sarea = drm_legacy_getsarea(dev);
  687. if (!dev_priv->sarea) {
  688. DRM_ERROR("failed to find sarea!\n");
  689. return -EINVAL;
  690. }
  691. if (!dev_priv->used_new_dma_init) {
  692. dev_priv->dma_access = MGA_PAGPXFER;
  693. dev_priv->wagp_enable = MGA_WAGP_ENABLE;
  694. dev_priv->status = drm_legacy_findmap(dev, init->status_offset);
  695. if (!dev_priv->status) {
  696. DRM_ERROR("failed to find status page!\n");
  697. return -EINVAL;
  698. }
  699. dev_priv->mmio = drm_legacy_findmap(dev, init->mmio_offset);
  700. if (!dev_priv->mmio) {
  701. DRM_ERROR("failed to find mmio region!\n");
  702. return -EINVAL;
  703. }
  704. dev_priv->warp = drm_legacy_findmap(dev, init->warp_offset);
  705. if (!dev_priv->warp) {
  706. DRM_ERROR("failed to find warp microcode region!\n");
  707. return -EINVAL;
  708. }
  709. dev_priv->primary = drm_legacy_findmap(dev, init->primary_offset);
  710. if (!dev_priv->primary) {
  711. DRM_ERROR("failed to find primary dma region!\n");
  712. return -EINVAL;
  713. }
  714. dev->agp_buffer_token = init->buffers_offset;
  715. dev->agp_buffer_map =
  716. drm_legacy_findmap(dev, init->buffers_offset);
  717. if (!dev->agp_buffer_map) {
  718. DRM_ERROR("failed to find dma buffer region!\n");
  719. return -EINVAL;
  720. }
  721. drm_legacy_ioremap(dev_priv->warp, dev);
  722. drm_legacy_ioremap(dev_priv->primary, dev);
  723. drm_legacy_ioremap(dev->agp_buffer_map, dev);
  724. }
  725. dev_priv->sarea_priv =
  726. (drm_mga_sarea_t *) ((u8 *) dev_priv->sarea->handle +
  727. init->sarea_priv_offset);
  728. if (!dev_priv->warp->handle ||
  729. !dev_priv->primary->handle ||
  730. ((dev_priv->dma_access != 0) &&
  731. ((dev->agp_buffer_map == NULL) ||
  732. (dev->agp_buffer_map->handle == NULL)))) {
  733. DRM_ERROR("failed to ioremap agp regions!\n");
  734. return -ENOMEM;
  735. }
  736. ret = mga_warp_install_microcode(dev_priv);
  737. if (ret < 0) {
  738. DRM_ERROR("failed to install WARP ucode!: %d\n", ret);
  739. return ret;
  740. }
  741. ret = mga_warp_init(dev_priv);
  742. if (ret < 0) {
  743. DRM_ERROR("failed to init WARP engine!: %d\n", ret);
  744. return ret;
  745. }
  746. dev_priv->prim.status = (u32 *) dev_priv->status->handle;
  747. mga_do_wait_for_idle(dev_priv);
  748. /* Init the primary DMA registers.
  749. */
  750. MGA_WRITE(MGA_PRIMADDRESS, dev_priv->primary->offset | MGA_DMA_GENERAL);
  751. #if 0
  752. MGA_WRITE(MGA_PRIMPTR, virt_to_bus((void *)dev_priv->prim.status) | MGA_PRIMPTREN0 | /* Soft trap, SECEND, SETUPEND */
  753. MGA_PRIMPTREN1); /* DWGSYNC */
  754. #endif
  755. dev_priv->prim.start = (u8 *) dev_priv->primary->handle;
  756. dev_priv->prim.end = ((u8 *) dev_priv->primary->handle
  757. + dev_priv->primary->size);
  758. dev_priv->prim.size = dev_priv->primary->size;
  759. dev_priv->prim.tail = 0;
  760. dev_priv->prim.space = dev_priv->prim.size;
  761. dev_priv->prim.wrapped = 0;
  762. dev_priv->prim.last_flush = 0;
  763. dev_priv->prim.last_wrap = 0;
  764. dev_priv->prim.high_mark = 256 * DMA_BLOCK_SIZE;
  765. dev_priv->prim.status[0] = dev_priv->primary->offset;
  766. dev_priv->prim.status[1] = 0;
  767. dev_priv->sarea_priv->last_wrap = 0;
  768. dev_priv->sarea_priv->last_frame.head = 0;
  769. dev_priv->sarea_priv->last_frame.wrap = 0;
  770. if (mga_freelist_init(dev, dev_priv) < 0) {
  771. DRM_ERROR("could not initialize freelist\n");
  772. return -ENOMEM;
  773. }
  774. return 0;
  775. }
  776. static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup)
  777. {
  778. int err = 0;
  779. DRM_DEBUG("\n");
  780. /* Make sure interrupts are disabled here because the uninstall ioctl
  781. * may not have been called from userspace and after dev_private
  782. * is freed, it's too late.
  783. */
  784. if (dev->irq_enabled)
  785. drm_irq_uninstall(dev);
  786. if (dev->dev_private) {
  787. drm_mga_private_t *dev_priv = dev->dev_private;
  788. if ((dev_priv->warp != NULL)
  789. && (dev_priv->warp->type != _DRM_CONSISTENT))
  790. drm_legacy_ioremapfree(dev_priv->warp, dev);
  791. if ((dev_priv->primary != NULL)
  792. && (dev_priv->primary->type != _DRM_CONSISTENT))
  793. drm_legacy_ioremapfree(dev_priv->primary, dev);
  794. if (dev->agp_buffer_map != NULL)
  795. drm_legacy_ioremapfree(dev->agp_buffer_map, dev);
  796. if (dev_priv->used_new_dma_init) {
  797. #if IS_ENABLED(CONFIG_AGP)
  798. if (dev_priv->agp_handle != 0) {
  799. struct drm_agp_binding unbind_req;
  800. struct drm_agp_buffer free_req;
  801. unbind_req.handle = dev_priv->agp_handle;
  802. drm_agp_unbind(dev, &unbind_req);
  803. free_req.handle = dev_priv->agp_handle;
  804. drm_agp_free(dev, &free_req);
  805. dev_priv->agp_textures = NULL;
  806. dev_priv->agp_size = 0;
  807. dev_priv->agp_handle = 0;
  808. }
  809. if ((dev->agp != NULL) && dev->agp->acquired)
  810. err = drm_agp_release(dev);
  811. #endif
  812. }
  813. dev_priv->warp = NULL;
  814. dev_priv->primary = NULL;
  815. dev_priv->sarea = NULL;
  816. dev_priv->sarea_priv = NULL;
  817. dev->agp_buffer_map = NULL;
  818. if (full_cleanup) {
  819. dev_priv->mmio = NULL;
  820. dev_priv->status = NULL;
  821. dev_priv->used_new_dma_init = 0;
  822. }
  823. memset(&dev_priv->prim, 0, sizeof(dev_priv->prim));
  824. dev_priv->warp_pipe = 0;
  825. memset(dev_priv->warp_pipe_phys, 0,
  826. sizeof(dev_priv->warp_pipe_phys));
  827. if (dev_priv->head != NULL)
  828. mga_freelist_cleanup(dev);
  829. }
  830. return err;
  831. }
  832. int mga_dma_init(struct drm_device *dev, void *data,
  833. struct drm_file *file_priv)
  834. {
  835. drm_mga_init_t *init = data;
  836. int err;
  837. LOCK_TEST_WITH_RETURN(dev, file_priv);
  838. switch (init->func) {
  839. case MGA_INIT_DMA:
  840. err = mga_do_init_dma(dev, init);
  841. if (err)
  842. (void)mga_do_cleanup_dma(dev, FULL_CLEANUP);
  843. return err;
  844. case MGA_CLEANUP_DMA:
  845. return mga_do_cleanup_dma(dev, FULL_CLEANUP);
  846. }
  847. return -EINVAL;
  848. }
  849. /* ================================================================
  850. * Primary DMA stream management
  851. */
  852. int mga_dma_flush(struct drm_device *dev, void *data,
  853. struct drm_file *file_priv)
  854. {
  855. drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
  856. struct drm_lock *lock = data;
  857. LOCK_TEST_WITH_RETURN(dev, file_priv);
  858. DRM_DEBUG("%s%s%s\n",
  859. (lock->flags & _DRM_LOCK_FLUSH) ? "flush, " : "",
  860. (lock->flags & _DRM_LOCK_FLUSH_ALL) ? "flush all, " : "",
  861. (lock->flags & _DRM_LOCK_QUIESCENT) ? "idle, " : "");
  862. WRAP_WAIT_WITH_RETURN(dev_priv);
  863. if (lock->flags & (_DRM_LOCK_FLUSH | _DRM_LOCK_FLUSH_ALL))
  864. mga_do_dma_flush(dev_priv);
  865. if (lock->flags & _DRM_LOCK_QUIESCENT) {
  866. #if MGA_DMA_DEBUG
  867. int ret = mga_do_wait_for_idle(dev_priv);
  868. if (ret < 0)
  869. DRM_INFO("-EBUSY\n");
  870. return ret;
  871. #else
  872. return mga_do_wait_for_idle(dev_priv);
  873. #endif
  874. } else {
  875. return 0;
  876. }
  877. }
  878. int mga_dma_reset(struct drm_device *dev, void *data,
  879. struct drm_file *file_priv)
  880. {
  881. drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
  882. LOCK_TEST_WITH_RETURN(dev, file_priv);
  883. return mga_do_dma_reset(dev_priv);
  884. }
  885. /* ================================================================
  886. * DMA buffer management
  887. */
  888. static int mga_dma_get_buffers(struct drm_device *dev,
  889. struct drm_file *file_priv, struct drm_dma *d)
  890. {
  891. struct drm_buf *buf;
  892. int i;
  893. for (i = d->granted_count; i < d->request_count; i++) {
  894. buf = mga_freelist_get(dev);
  895. if (!buf)
  896. return -EAGAIN;
  897. buf->file_priv = file_priv;
  898. if (copy_to_user(&d->request_indices[i],
  899. &buf->idx, sizeof(buf->idx)))
  900. return -EFAULT;
  901. if (copy_to_user(&d->request_sizes[i],
  902. &buf->total, sizeof(buf->total)))
  903. return -EFAULT;
  904. d->granted_count++;
  905. }
  906. return 0;
  907. }
  908. int mga_dma_buffers(struct drm_device *dev, void *data,
  909. struct drm_file *file_priv)
  910. {
  911. struct drm_device_dma *dma = dev->dma;
  912. drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
  913. struct drm_dma *d = data;
  914. int ret = 0;
  915. LOCK_TEST_WITH_RETURN(dev, file_priv);
  916. /* Please don't send us buffers.
  917. */
  918. if (d->send_count != 0) {
  919. DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
  920. DRM_CURRENTPID, d->send_count);
  921. return -EINVAL;
  922. }
  923. /* We'll send you buffers.
  924. */
  925. if (d->request_count < 0 || d->request_count > dma->buf_count) {
  926. DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
  927. DRM_CURRENTPID, d->request_count, dma->buf_count);
  928. return -EINVAL;
  929. }
  930. WRAP_TEST_WITH_RETURN(dev_priv);
  931. d->granted_count = 0;
  932. if (d->request_count)
  933. ret = mga_dma_get_buffers(dev, file_priv, d);
  934. return ret;
  935. }
  936. /**
  937. * Called just before the module is unloaded.
  938. */
  939. void mga_driver_unload(struct drm_device *dev)
  940. {
  941. kfree(dev->dev_private);
  942. dev->dev_private = NULL;
  943. }
  944. /**
  945. * Called when the last opener of the device is closed.
  946. */
  947. void mga_driver_lastclose(struct drm_device *dev)
  948. {
  949. mga_do_cleanup_dma(dev, FULL_CLEANUP);
  950. }
  951. int mga_driver_dma_quiescent(struct drm_device *dev)
  952. {
  953. drm_mga_private_t *dev_priv = dev->dev_private;
  954. return mga_do_wait_for_idle(dev_priv);
  955. }