savage_bci.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /* savage_bci.c -- BCI support for Savage
  2. *
  3. * Copyright 2004 Felix Kuehling
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NON-INFRINGEMENT. IN NO EVENT SHALL FELIX KUEHLING BE LIABLE FOR
  21. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  22. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <drm/drmP.h>
  26. #include <drm/savage_drm.h>
  27. #include "savage_drv.h"
  28. /* Need a long timeout for shadow status updates can take a while
  29. * and so can waiting for events when the queue is full. */
  30. #define SAVAGE_DEFAULT_USEC_TIMEOUT 1000000 /* 1s */
  31. #define SAVAGE_EVENT_USEC_TIMEOUT 5000000 /* 5s */
  32. #define SAVAGE_FREELIST_DEBUG 0
  33. static int savage_do_cleanup_bci(struct drm_device *dev);
  34. static int
  35. savage_bci_wait_fifo_shadow(drm_savage_private_t * dev_priv, unsigned int n)
  36. {
  37. uint32_t mask = dev_priv->status_used_mask;
  38. uint32_t threshold = dev_priv->bci_threshold_hi;
  39. uint32_t status;
  40. int i;
  41. #if SAVAGE_BCI_DEBUG
  42. if (n > dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - threshold)
  43. DRM_ERROR("Trying to emit %d words "
  44. "(more than guaranteed space in COB)\n", n);
  45. #endif
  46. for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
  47. mb();
  48. status = dev_priv->status_ptr[0];
  49. if ((status & mask) < threshold)
  50. return 0;
  51. DRM_UDELAY(1);
  52. }
  53. #if SAVAGE_BCI_DEBUG
  54. DRM_ERROR("failed!\n");
  55. DRM_INFO(" status=0x%08x, threshold=0x%08x\n", status, threshold);
  56. #endif
  57. return -EBUSY;
  58. }
  59. static int
  60. savage_bci_wait_fifo_s3d(drm_savage_private_t * dev_priv, unsigned int n)
  61. {
  62. uint32_t maxUsed = dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - n;
  63. uint32_t status;
  64. int i;
  65. for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
  66. status = SAVAGE_READ(SAVAGE_STATUS_WORD0);
  67. if ((status & SAVAGE_FIFO_USED_MASK_S3D) <= maxUsed)
  68. return 0;
  69. DRM_UDELAY(1);
  70. }
  71. #if SAVAGE_BCI_DEBUG
  72. DRM_ERROR("failed!\n");
  73. DRM_INFO(" status=0x%08x\n", status);
  74. #endif
  75. return -EBUSY;
  76. }
  77. static int
  78. savage_bci_wait_fifo_s4(drm_savage_private_t * dev_priv, unsigned int n)
  79. {
  80. uint32_t maxUsed = dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - n;
  81. uint32_t status;
  82. int i;
  83. for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
  84. status = SAVAGE_READ(SAVAGE_ALT_STATUS_WORD0);
  85. if ((status & SAVAGE_FIFO_USED_MASK_S4) <= maxUsed)
  86. return 0;
  87. DRM_UDELAY(1);
  88. }
  89. #if SAVAGE_BCI_DEBUG
  90. DRM_ERROR("failed!\n");
  91. DRM_INFO(" status=0x%08x\n", status);
  92. #endif
  93. return -EBUSY;
  94. }
  95. /*
  96. * Waiting for events.
  97. *
  98. * The BIOSresets the event tag to 0 on mode changes. Therefore we
  99. * never emit 0 to the event tag. If we find a 0 event tag we know the
  100. * BIOS stomped on it and return success assuming that the BIOS waited
  101. * for engine idle.
  102. *
  103. * Note: if the Xserver uses the event tag it has to follow the same
  104. * rule. Otherwise there may be glitches every 2^16 events.
  105. */
  106. static int
  107. savage_bci_wait_event_shadow(drm_savage_private_t * dev_priv, uint16_t e)
  108. {
  109. uint32_t status;
  110. int i;
  111. for (i = 0; i < SAVAGE_EVENT_USEC_TIMEOUT; i++) {
  112. mb();
  113. status = dev_priv->status_ptr[1];
  114. if ((((status & 0xffff) - e) & 0xffff) <= 0x7fff ||
  115. (status & 0xffff) == 0)
  116. return 0;
  117. DRM_UDELAY(1);
  118. }
  119. #if SAVAGE_BCI_DEBUG
  120. DRM_ERROR("failed!\n");
  121. DRM_INFO(" status=0x%08x, e=0x%04x\n", status, e);
  122. #endif
  123. return -EBUSY;
  124. }
  125. static int
  126. savage_bci_wait_event_reg(drm_savage_private_t * dev_priv, uint16_t e)
  127. {
  128. uint32_t status;
  129. int i;
  130. for (i = 0; i < SAVAGE_EVENT_USEC_TIMEOUT; i++) {
  131. status = SAVAGE_READ(SAVAGE_STATUS_WORD1);
  132. if ((((status & 0xffff) - e) & 0xffff) <= 0x7fff ||
  133. (status & 0xffff) == 0)
  134. return 0;
  135. DRM_UDELAY(1);
  136. }
  137. #if SAVAGE_BCI_DEBUG
  138. DRM_ERROR("failed!\n");
  139. DRM_INFO(" status=0x%08x, e=0x%04x\n", status, e);
  140. #endif
  141. return -EBUSY;
  142. }
  143. uint16_t savage_bci_emit_event(drm_savage_private_t * dev_priv,
  144. unsigned int flags)
  145. {
  146. uint16_t count;
  147. BCI_LOCALS;
  148. if (dev_priv->status_ptr) {
  149. /* coordinate with Xserver */
  150. count = dev_priv->status_ptr[1023];
  151. if (count < dev_priv->event_counter)
  152. dev_priv->event_wrap++;
  153. } else {
  154. count = dev_priv->event_counter;
  155. }
  156. count = (count + 1) & 0xffff;
  157. if (count == 0) {
  158. count++; /* See the comment above savage_wait_event_*. */
  159. dev_priv->event_wrap++;
  160. }
  161. dev_priv->event_counter = count;
  162. if (dev_priv->status_ptr)
  163. dev_priv->status_ptr[1023] = (uint32_t) count;
  164. if ((flags & (SAVAGE_WAIT_2D | SAVAGE_WAIT_3D))) {
  165. unsigned int wait_cmd = BCI_CMD_WAIT;
  166. if ((flags & SAVAGE_WAIT_2D))
  167. wait_cmd |= BCI_CMD_WAIT_2D;
  168. if ((flags & SAVAGE_WAIT_3D))
  169. wait_cmd |= BCI_CMD_WAIT_3D;
  170. BEGIN_BCI(2);
  171. BCI_WRITE(wait_cmd);
  172. } else {
  173. BEGIN_BCI(1);
  174. }
  175. BCI_WRITE(BCI_CMD_UPDATE_EVENT_TAG | (uint32_t) count);
  176. return count;
  177. }
  178. /*
  179. * Freelist management
  180. */
  181. static int savage_freelist_init(struct drm_device * dev)
  182. {
  183. drm_savage_private_t *dev_priv = dev->dev_private;
  184. struct drm_device_dma *dma = dev->dma;
  185. struct drm_buf *buf;
  186. drm_savage_buf_priv_t *entry;
  187. int i;
  188. DRM_DEBUG("count=%d\n", dma->buf_count);
  189. dev_priv->head.next = &dev_priv->tail;
  190. dev_priv->head.prev = NULL;
  191. dev_priv->head.buf = NULL;
  192. dev_priv->tail.next = NULL;
  193. dev_priv->tail.prev = &dev_priv->head;
  194. dev_priv->tail.buf = NULL;
  195. for (i = 0; i < dma->buf_count; i++) {
  196. buf = dma->buflist[i];
  197. entry = buf->dev_private;
  198. SET_AGE(&entry->age, 0, 0);
  199. entry->buf = buf;
  200. entry->next = dev_priv->head.next;
  201. entry->prev = &dev_priv->head;
  202. dev_priv->head.next->prev = entry;
  203. dev_priv->head.next = entry;
  204. }
  205. return 0;
  206. }
  207. static struct drm_buf *savage_freelist_get(struct drm_device * dev)
  208. {
  209. drm_savage_private_t *dev_priv = dev->dev_private;
  210. drm_savage_buf_priv_t *tail = dev_priv->tail.prev;
  211. uint16_t event;
  212. unsigned int wrap;
  213. DRM_DEBUG("\n");
  214. UPDATE_EVENT_COUNTER();
  215. if (dev_priv->status_ptr)
  216. event = dev_priv->status_ptr[1] & 0xffff;
  217. else
  218. event = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
  219. wrap = dev_priv->event_wrap;
  220. if (event > dev_priv->event_counter)
  221. wrap--; /* hardware hasn't passed the last wrap yet */
  222. DRM_DEBUG(" tail=0x%04x %d\n", tail->age.event, tail->age.wrap);
  223. DRM_DEBUG(" head=0x%04x %d\n", event, wrap);
  224. if (tail->buf && (TEST_AGE(&tail->age, event, wrap) || event == 0)) {
  225. drm_savage_buf_priv_t *next = tail->next;
  226. drm_savage_buf_priv_t *prev = tail->prev;
  227. prev->next = next;
  228. next->prev = prev;
  229. tail->next = tail->prev = NULL;
  230. return tail->buf;
  231. }
  232. DRM_DEBUG("returning NULL, tail->buf=%p!\n", tail->buf);
  233. return NULL;
  234. }
  235. void savage_freelist_put(struct drm_device * dev, struct drm_buf * buf)
  236. {
  237. drm_savage_private_t *dev_priv = dev->dev_private;
  238. drm_savage_buf_priv_t *entry = buf->dev_private, *prev, *next;
  239. DRM_DEBUG("age=0x%04x wrap=%d\n", entry->age.event, entry->age.wrap);
  240. if (entry->next != NULL || entry->prev != NULL) {
  241. DRM_ERROR("entry already on freelist.\n");
  242. return;
  243. }
  244. prev = &dev_priv->head;
  245. next = prev->next;
  246. prev->next = entry;
  247. next->prev = entry;
  248. entry->prev = prev;
  249. entry->next = next;
  250. }
  251. /*
  252. * Command DMA
  253. */
  254. static int savage_dma_init(drm_savage_private_t * dev_priv)
  255. {
  256. unsigned int i;
  257. dev_priv->nr_dma_pages = dev_priv->cmd_dma->size /
  258. (SAVAGE_DMA_PAGE_SIZE * 4);
  259. dev_priv->dma_pages = kmalloc(sizeof(drm_savage_dma_page_t) *
  260. dev_priv->nr_dma_pages, GFP_KERNEL);
  261. if (dev_priv->dma_pages == NULL)
  262. return -ENOMEM;
  263. for (i = 0; i < dev_priv->nr_dma_pages; ++i) {
  264. SET_AGE(&dev_priv->dma_pages[i].age, 0, 0);
  265. dev_priv->dma_pages[i].used = 0;
  266. dev_priv->dma_pages[i].flushed = 0;
  267. }
  268. SET_AGE(&dev_priv->last_dma_age, 0, 0);
  269. dev_priv->first_dma_page = 0;
  270. dev_priv->current_dma_page = 0;
  271. return 0;
  272. }
  273. void savage_dma_reset(drm_savage_private_t * dev_priv)
  274. {
  275. uint16_t event;
  276. unsigned int wrap, i;
  277. event = savage_bci_emit_event(dev_priv, 0);
  278. wrap = dev_priv->event_wrap;
  279. for (i = 0; i < dev_priv->nr_dma_pages; ++i) {
  280. SET_AGE(&dev_priv->dma_pages[i].age, event, wrap);
  281. dev_priv->dma_pages[i].used = 0;
  282. dev_priv->dma_pages[i].flushed = 0;
  283. }
  284. SET_AGE(&dev_priv->last_dma_age, event, wrap);
  285. dev_priv->first_dma_page = dev_priv->current_dma_page = 0;
  286. }
  287. void savage_dma_wait(drm_savage_private_t * dev_priv, unsigned int page)
  288. {
  289. uint16_t event;
  290. unsigned int wrap;
  291. /* Faked DMA buffer pages don't age. */
  292. if (dev_priv->cmd_dma == &dev_priv->fake_dma)
  293. return;
  294. UPDATE_EVENT_COUNTER();
  295. if (dev_priv->status_ptr)
  296. event = dev_priv->status_ptr[1] & 0xffff;
  297. else
  298. event = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
  299. wrap = dev_priv->event_wrap;
  300. if (event > dev_priv->event_counter)
  301. wrap--; /* hardware hasn't passed the last wrap yet */
  302. if (dev_priv->dma_pages[page].age.wrap > wrap ||
  303. (dev_priv->dma_pages[page].age.wrap == wrap &&
  304. dev_priv->dma_pages[page].age.event > event)) {
  305. if (dev_priv->wait_evnt(dev_priv,
  306. dev_priv->dma_pages[page].age.event)
  307. < 0)
  308. DRM_ERROR("wait_evnt failed!\n");
  309. }
  310. }
  311. uint32_t *savage_dma_alloc(drm_savage_private_t * dev_priv, unsigned int n)
  312. {
  313. unsigned int cur = dev_priv->current_dma_page;
  314. unsigned int rest = SAVAGE_DMA_PAGE_SIZE -
  315. dev_priv->dma_pages[cur].used;
  316. unsigned int nr_pages = (n - rest + SAVAGE_DMA_PAGE_SIZE - 1) /
  317. SAVAGE_DMA_PAGE_SIZE;
  318. uint32_t *dma_ptr;
  319. unsigned int i;
  320. DRM_DEBUG("cur=%u, cur->used=%u, n=%u, rest=%u, nr_pages=%u\n",
  321. cur, dev_priv->dma_pages[cur].used, n, rest, nr_pages);
  322. if (cur + nr_pages < dev_priv->nr_dma_pages) {
  323. dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
  324. cur * SAVAGE_DMA_PAGE_SIZE + dev_priv->dma_pages[cur].used;
  325. if (n < rest)
  326. rest = n;
  327. dev_priv->dma_pages[cur].used += rest;
  328. n -= rest;
  329. cur++;
  330. } else {
  331. dev_priv->dma_flush(dev_priv);
  332. nr_pages =
  333. (n + SAVAGE_DMA_PAGE_SIZE - 1) / SAVAGE_DMA_PAGE_SIZE;
  334. for (i = cur; i < dev_priv->nr_dma_pages; ++i) {
  335. dev_priv->dma_pages[i].age = dev_priv->last_dma_age;
  336. dev_priv->dma_pages[i].used = 0;
  337. dev_priv->dma_pages[i].flushed = 0;
  338. }
  339. dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle;
  340. dev_priv->first_dma_page = cur = 0;
  341. }
  342. for (i = cur; nr_pages > 0; ++i, --nr_pages) {
  343. #if SAVAGE_DMA_DEBUG
  344. if (dev_priv->dma_pages[i].used) {
  345. DRM_ERROR("unflushed page %u: used=%u\n",
  346. i, dev_priv->dma_pages[i].used);
  347. }
  348. #endif
  349. if (n > SAVAGE_DMA_PAGE_SIZE)
  350. dev_priv->dma_pages[i].used = SAVAGE_DMA_PAGE_SIZE;
  351. else
  352. dev_priv->dma_pages[i].used = n;
  353. n -= SAVAGE_DMA_PAGE_SIZE;
  354. }
  355. dev_priv->current_dma_page = --i;
  356. DRM_DEBUG("cur=%u, cur->used=%u, n=%u\n",
  357. i, dev_priv->dma_pages[i].used, n);
  358. savage_dma_wait(dev_priv, dev_priv->current_dma_page);
  359. return dma_ptr;
  360. }
  361. static void savage_dma_flush(drm_savage_private_t * dev_priv)
  362. {
  363. unsigned int first = dev_priv->first_dma_page;
  364. unsigned int cur = dev_priv->current_dma_page;
  365. uint16_t event;
  366. unsigned int wrap, pad, align, len, i;
  367. unsigned long phys_addr;
  368. BCI_LOCALS;
  369. if (first == cur &&
  370. dev_priv->dma_pages[cur].used == dev_priv->dma_pages[cur].flushed)
  371. return;
  372. /* pad length to multiples of 2 entries
  373. * align start of next DMA block to multiles of 8 entries */
  374. pad = -dev_priv->dma_pages[cur].used & 1;
  375. align = -(dev_priv->dma_pages[cur].used + pad) & 7;
  376. DRM_DEBUG("first=%u, cur=%u, first->flushed=%u, cur->used=%u, "
  377. "pad=%u, align=%u\n",
  378. first, cur, dev_priv->dma_pages[first].flushed,
  379. dev_priv->dma_pages[cur].used, pad, align);
  380. /* pad with noops */
  381. if (pad) {
  382. uint32_t *dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
  383. cur * SAVAGE_DMA_PAGE_SIZE + dev_priv->dma_pages[cur].used;
  384. dev_priv->dma_pages[cur].used += pad;
  385. while (pad != 0) {
  386. *dma_ptr++ = BCI_CMD_WAIT;
  387. pad--;
  388. }
  389. }
  390. mb();
  391. /* do flush ... */
  392. phys_addr = dev_priv->cmd_dma->offset +
  393. (first * SAVAGE_DMA_PAGE_SIZE +
  394. dev_priv->dma_pages[first].flushed) * 4;
  395. len = (cur - first) * SAVAGE_DMA_PAGE_SIZE +
  396. dev_priv->dma_pages[cur].used - dev_priv->dma_pages[first].flushed;
  397. DRM_DEBUG("phys_addr=%lx, len=%u\n",
  398. phys_addr | dev_priv->dma_type, len);
  399. BEGIN_BCI(3);
  400. BCI_SET_REGISTERS(SAVAGE_DMABUFADDR, 1);
  401. BCI_WRITE(phys_addr | dev_priv->dma_type);
  402. BCI_DMA(len);
  403. /* fix alignment of the start of the next block */
  404. dev_priv->dma_pages[cur].used += align;
  405. /* age DMA pages */
  406. event = savage_bci_emit_event(dev_priv, 0);
  407. wrap = dev_priv->event_wrap;
  408. for (i = first; i < cur; ++i) {
  409. SET_AGE(&dev_priv->dma_pages[i].age, event, wrap);
  410. dev_priv->dma_pages[i].used = 0;
  411. dev_priv->dma_pages[i].flushed = 0;
  412. }
  413. /* age the current page only when it's full */
  414. if (dev_priv->dma_pages[cur].used == SAVAGE_DMA_PAGE_SIZE) {
  415. SET_AGE(&dev_priv->dma_pages[cur].age, event, wrap);
  416. dev_priv->dma_pages[cur].used = 0;
  417. dev_priv->dma_pages[cur].flushed = 0;
  418. /* advance to next page */
  419. cur++;
  420. if (cur == dev_priv->nr_dma_pages)
  421. cur = 0;
  422. dev_priv->first_dma_page = dev_priv->current_dma_page = cur;
  423. } else {
  424. dev_priv->first_dma_page = cur;
  425. dev_priv->dma_pages[cur].flushed = dev_priv->dma_pages[i].used;
  426. }
  427. SET_AGE(&dev_priv->last_dma_age, event, wrap);
  428. DRM_DEBUG("first=cur=%u, cur->used=%u, cur->flushed=%u\n", cur,
  429. dev_priv->dma_pages[cur].used,
  430. dev_priv->dma_pages[cur].flushed);
  431. }
  432. static void savage_fake_dma_flush(drm_savage_private_t * dev_priv)
  433. {
  434. unsigned int i, j;
  435. BCI_LOCALS;
  436. if (dev_priv->first_dma_page == dev_priv->current_dma_page &&
  437. dev_priv->dma_pages[dev_priv->current_dma_page].used == 0)
  438. return;
  439. DRM_DEBUG("first=%u, cur=%u, cur->used=%u\n",
  440. dev_priv->first_dma_page, dev_priv->current_dma_page,
  441. dev_priv->dma_pages[dev_priv->current_dma_page].used);
  442. for (i = dev_priv->first_dma_page;
  443. i <= dev_priv->current_dma_page && dev_priv->dma_pages[i].used;
  444. ++i) {
  445. uint32_t *dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
  446. i * SAVAGE_DMA_PAGE_SIZE;
  447. #if SAVAGE_DMA_DEBUG
  448. /* Sanity check: all pages except the last one must be full. */
  449. if (i < dev_priv->current_dma_page &&
  450. dev_priv->dma_pages[i].used != SAVAGE_DMA_PAGE_SIZE) {
  451. DRM_ERROR("partial DMA page %u: used=%u",
  452. i, dev_priv->dma_pages[i].used);
  453. }
  454. #endif
  455. BEGIN_BCI(dev_priv->dma_pages[i].used);
  456. for (j = 0; j < dev_priv->dma_pages[i].used; ++j) {
  457. BCI_WRITE(dma_ptr[j]);
  458. }
  459. dev_priv->dma_pages[i].used = 0;
  460. }
  461. /* reset to first page */
  462. dev_priv->first_dma_page = dev_priv->current_dma_page = 0;
  463. }
  464. int savage_driver_load(struct drm_device *dev, unsigned long chipset)
  465. {
  466. drm_savage_private_t *dev_priv;
  467. dev_priv = kzalloc(sizeof(drm_savage_private_t), GFP_KERNEL);
  468. if (dev_priv == NULL)
  469. return -ENOMEM;
  470. dev->dev_private = (void *)dev_priv;
  471. dev_priv->chipset = (enum savage_family)chipset;
  472. pci_set_master(dev->pdev);
  473. return 0;
  474. }
  475. /*
  476. * Initialize mappings. On Savage4 and SavageIX the alignment
  477. * and size of the aperture is not suitable for automatic MTRR setup
  478. * in drm_legacy_addmap. Therefore we add them manually before the maps are
  479. * initialized, and tear them down on last close.
  480. */
  481. int savage_driver_firstopen(struct drm_device *dev)
  482. {
  483. drm_savage_private_t *dev_priv = dev->dev_private;
  484. unsigned long mmio_base, fb_base, fb_size, aperture_base;
  485. /* fb_rsrc and aper_rsrc aren't really used currently, but still exist
  486. * in case we decide we need information on the BAR for BSD in the
  487. * future.
  488. */
  489. unsigned int fb_rsrc, aper_rsrc;
  490. int ret = 0;
  491. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  492. fb_rsrc = 0;
  493. fb_base = pci_resource_start(dev->pdev, 0);
  494. fb_size = SAVAGE_FB_SIZE_S3;
  495. mmio_base = fb_base + SAVAGE_FB_SIZE_S3;
  496. aper_rsrc = 0;
  497. aperture_base = fb_base + SAVAGE_APERTURE_OFFSET;
  498. /* this should always be true */
  499. if (pci_resource_len(dev->pdev, 0) == 0x08000000) {
  500. /* Don't make MMIO write-cobining! We need 3
  501. * MTRRs. */
  502. dev_priv->mtrr_handles[0] =
  503. arch_phys_wc_add(fb_base, 0x01000000);
  504. dev_priv->mtrr_handles[1] =
  505. arch_phys_wc_add(fb_base + 0x02000000,
  506. 0x02000000);
  507. dev_priv->mtrr_handles[2] =
  508. arch_phys_wc_add(fb_base + 0x04000000,
  509. 0x04000000);
  510. } else {
  511. DRM_ERROR("strange pci_resource_len %08llx\n",
  512. (unsigned long long)
  513. pci_resource_len(dev->pdev, 0));
  514. }
  515. } else if (dev_priv->chipset != S3_SUPERSAVAGE &&
  516. dev_priv->chipset != S3_SAVAGE2000) {
  517. mmio_base = pci_resource_start(dev->pdev, 0);
  518. fb_rsrc = 1;
  519. fb_base = pci_resource_start(dev->pdev, 1);
  520. fb_size = SAVAGE_FB_SIZE_S4;
  521. aper_rsrc = 1;
  522. aperture_base = fb_base + SAVAGE_APERTURE_OFFSET;
  523. /* this should always be true */
  524. if (pci_resource_len(dev->pdev, 1) == 0x08000000) {
  525. /* Can use one MTRR to cover both fb and
  526. * aperture. */
  527. dev_priv->mtrr_handles[0] =
  528. arch_phys_wc_add(fb_base,
  529. 0x08000000);
  530. } else {
  531. DRM_ERROR("strange pci_resource_len %08llx\n",
  532. (unsigned long long)
  533. pci_resource_len(dev->pdev, 1));
  534. }
  535. } else {
  536. mmio_base = pci_resource_start(dev->pdev, 0);
  537. fb_rsrc = 1;
  538. fb_base = pci_resource_start(dev->pdev, 1);
  539. fb_size = pci_resource_len(dev->pdev, 1);
  540. aper_rsrc = 2;
  541. aperture_base = pci_resource_start(dev->pdev, 2);
  542. /* Automatic MTRR setup will do the right thing. */
  543. }
  544. ret = drm_legacy_addmap(dev, mmio_base, SAVAGE_MMIO_SIZE,
  545. _DRM_REGISTERS, _DRM_READ_ONLY,
  546. &dev_priv->mmio);
  547. if (ret)
  548. return ret;
  549. ret = drm_legacy_addmap(dev, fb_base, fb_size, _DRM_FRAME_BUFFER,
  550. _DRM_WRITE_COMBINING, &dev_priv->fb);
  551. if (ret)
  552. return ret;
  553. ret = drm_legacy_addmap(dev, aperture_base, SAVAGE_APERTURE_SIZE,
  554. _DRM_FRAME_BUFFER, _DRM_WRITE_COMBINING,
  555. &dev_priv->aperture);
  556. return ret;
  557. }
  558. /*
  559. * Delete MTRRs and free device-private data.
  560. */
  561. void savage_driver_lastclose(struct drm_device *dev)
  562. {
  563. drm_savage_private_t *dev_priv = dev->dev_private;
  564. int i;
  565. for (i = 0; i < 3; ++i) {
  566. arch_phys_wc_del(dev_priv->mtrr_handles[i]);
  567. dev_priv->mtrr_handles[i] = 0;
  568. }
  569. }
  570. int savage_driver_unload(struct drm_device *dev)
  571. {
  572. drm_savage_private_t *dev_priv = dev->dev_private;
  573. kfree(dev_priv);
  574. return 0;
  575. }
  576. static int savage_do_init_bci(struct drm_device * dev, drm_savage_init_t * init)
  577. {
  578. drm_savage_private_t *dev_priv = dev->dev_private;
  579. if (init->fb_bpp != 16 && init->fb_bpp != 32) {
  580. DRM_ERROR("invalid frame buffer bpp %d!\n", init->fb_bpp);
  581. return -EINVAL;
  582. }
  583. if (init->depth_bpp != 16 && init->depth_bpp != 32) {
  584. DRM_ERROR("invalid depth buffer bpp %d!\n", init->fb_bpp);
  585. return -EINVAL;
  586. }
  587. if (init->dma_type != SAVAGE_DMA_AGP &&
  588. init->dma_type != SAVAGE_DMA_PCI) {
  589. DRM_ERROR("invalid dma memory type %d!\n", init->dma_type);
  590. return -EINVAL;
  591. }
  592. dev_priv->cob_size = init->cob_size;
  593. dev_priv->bci_threshold_lo = init->bci_threshold_lo;
  594. dev_priv->bci_threshold_hi = init->bci_threshold_hi;
  595. dev_priv->dma_type = init->dma_type;
  596. dev_priv->fb_bpp = init->fb_bpp;
  597. dev_priv->front_offset = init->front_offset;
  598. dev_priv->front_pitch = init->front_pitch;
  599. dev_priv->back_offset = init->back_offset;
  600. dev_priv->back_pitch = init->back_pitch;
  601. dev_priv->depth_bpp = init->depth_bpp;
  602. dev_priv->depth_offset = init->depth_offset;
  603. dev_priv->depth_pitch = init->depth_pitch;
  604. dev_priv->texture_offset = init->texture_offset;
  605. dev_priv->texture_size = init->texture_size;
  606. dev_priv->sarea = drm_legacy_getsarea(dev);
  607. if (!dev_priv->sarea) {
  608. DRM_ERROR("could not find sarea!\n");
  609. savage_do_cleanup_bci(dev);
  610. return -EINVAL;
  611. }
  612. if (init->status_offset != 0) {
  613. dev_priv->status = drm_legacy_findmap(dev, init->status_offset);
  614. if (!dev_priv->status) {
  615. DRM_ERROR("could not find shadow status region!\n");
  616. savage_do_cleanup_bci(dev);
  617. return -EINVAL;
  618. }
  619. } else {
  620. dev_priv->status = NULL;
  621. }
  622. if (dev_priv->dma_type == SAVAGE_DMA_AGP && init->buffers_offset) {
  623. dev->agp_buffer_token = init->buffers_offset;
  624. dev->agp_buffer_map = drm_legacy_findmap(dev,
  625. init->buffers_offset);
  626. if (!dev->agp_buffer_map) {
  627. DRM_ERROR("could not find DMA buffer region!\n");
  628. savage_do_cleanup_bci(dev);
  629. return -EINVAL;
  630. }
  631. drm_legacy_ioremap(dev->agp_buffer_map, dev);
  632. if (!dev->agp_buffer_map->handle) {
  633. DRM_ERROR("failed to ioremap DMA buffer region!\n");
  634. savage_do_cleanup_bci(dev);
  635. return -ENOMEM;
  636. }
  637. }
  638. if (init->agp_textures_offset) {
  639. dev_priv->agp_textures =
  640. drm_legacy_findmap(dev, init->agp_textures_offset);
  641. if (!dev_priv->agp_textures) {
  642. DRM_ERROR("could not find agp texture region!\n");
  643. savage_do_cleanup_bci(dev);
  644. return -EINVAL;
  645. }
  646. } else {
  647. dev_priv->agp_textures = NULL;
  648. }
  649. if (init->cmd_dma_offset) {
  650. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  651. DRM_ERROR("command DMA not supported on "
  652. "Savage3D/MX/IX.\n");
  653. savage_do_cleanup_bci(dev);
  654. return -EINVAL;
  655. }
  656. if (dev->dma && dev->dma->buflist) {
  657. DRM_ERROR("command and vertex DMA not supported "
  658. "at the same time.\n");
  659. savage_do_cleanup_bci(dev);
  660. return -EINVAL;
  661. }
  662. dev_priv->cmd_dma = drm_legacy_findmap(dev, init->cmd_dma_offset);
  663. if (!dev_priv->cmd_dma) {
  664. DRM_ERROR("could not find command DMA region!\n");
  665. savage_do_cleanup_bci(dev);
  666. return -EINVAL;
  667. }
  668. if (dev_priv->dma_type == SAVAGE_DMA_AGP) {
  669. if (dev_priv->cmd_dma->type != _DRM_AGP) {
  670. DRM_ERROR("AGP command DMA region is not a "
  671. "_DRM_AGP map!\n");
  672. savage_do_cleanup_bci(dev);
  673. return -EINVAL;
  674. }
  675. drm_legacy_ioremap(dev_priv->cmd_dma, dev);
  676. if (!dev_priv->cmd_dma->handle) {
  677. DRM_ERROR("failed to ioremap command "
  678. "DMA region!\n");
  679. savage_do_cleanup_bci(dev);
  680. return -ENOMEM;
  681. }
  682. } else if (dev_priv->cmd_dma->type != _DRM_CONSISTENT) {
  683. DRM_ERROR("PCI command DMA region is not a "
  684. "_DRM_CONSISTENT map!\n");
  685. savage_do_cleanup_bci(dev);
  686. return -EINVAL;
  687. }
  688. } else {
  689. dev_priv->cmd_dma = NULL;
  690. }
  691. dev_priv->dma_flush = savage_dma_flush;
  692. if (!dev_priv->cmd_dma) {
  693. DRM_DEBUG("falling back to faked command DMA.\n");
  694. dev_priv->fake_dma.offset = 0;
  695. dev_priv->fake_dma.size = SAVAGE_FAKE_DMA_SIZE;
  696. dev_priv->fake_dma.type = _DRM_SHM;
  697. dev_priv->fake_dma.handle = kmalloc(SAVAGE_FAKE_DMA_SIZE,
  698. GFP_KERNEL);
  699. if (!dev_priv->fake_dma.handle) {
  700. DRM_ERROR("could not allocate faked DMA buffer!\n");
  701. savage_do_cleanup_bci(dev);
  702. return -ENOMEM;
  703. }
  704. dev_priv->cmd_dma = &dev_priv->fake_dma;
  705. dev_priv->dma_flush = savage_fake_dma_flush;
  706. }
  707. dev_priv->sarea_priv =
  708. (drm_savage_sarea_t *) ((uint8_t *) dev_priv->sarea->handle +
  709. init->sarea_priv_offset);
  710. /* setup bitmap descriptors */
  711. {
  712. unsigned int color_tile_format;
  713. unsigned int depth_tile_format;
  714. unsigned int front_stride, back_stride, depth_stride;
  715. if (dev_priv->chipset <= S3_SAVAGE4) {
  716. color_tile_format = dev_priv->fb_bpp == 16 ?
  717. SAVAGE_BD_TILE_16BPP : SAVAGE_BD_TILE_32BPP;
  718. depth_tile_format = dev_priv->depth_bpp == 16 ?
  719. SAVAGE_BD_TILE_16BPP : SAVAGE_BD_TILE_32BPP;
  720. } else {
  721. color_tile_format = SAVAGE_BD_TILE_DEST;
  722. depth_tile_format = SAVAGE_BD_TILE_DEST;
  723. }
  724. front_stride = dev_priv->front_pitch / (dev_priv->fb_bpp / 8);
  725. back_stride = dev_priv->back_pitch / (dev_priv->fb_bpp / 8);
  726. depth_stride =
  727. dev_priv->depth_pitch / (dev_priv->depth_bpp / 8);
  728. dev_priv->front_bd = front_stride | SAVAGE_BD_BW_DISABLE |
  729. (dev_priv->fb_bpp << SAVAGE_BD_BPP_SHIFT) |
  730. (color_tile_format << SAVAGE_BD_TILE_SHIFT);
  731. dev_priv->back_bd = back_stride | SAVAGE_BD_BW_DISABLE |
  732. (dev_priv->fb_bpp << SAVAGE_BD_BPP_SHIFT) |
  733. (color_tile_format << SAVAGE_BD_TILE_SHIFT);
  734. dev_priv->depth_bd = depth_stride | SAVAGE_BD_BW_DISABLE |
  735. (dev_priv->depth_bpp << SAVAGE_BD_BPP_SHIFT) |
  736. (depth_tile_format << SAVAGE_BD_TILE_SHIFT);
  737. }
  738. /* setup status and bci ptr */
  739. dev_priv->event_counter = 0;
  740. dev_priv->event_wrap = 0;
  741. dev_priv->bci_ptr = (volatile uint32_t *)
  742. ((uint8_t *) dev_priv->mmio->handle + SAVAGE_BCI_OFFSET);
  743. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  744. dev_priv->status_used_mask = SAVAGE_FIFO_USED_MASK_S3D;
  745. } else {
  746. dev_priv->status_used_mask = SAVAGE_FIFO_USED_MASK_S4;
  747. }
  748. if (dev_priv->status != NULL) {
  749. dev_priv->status_ptr =
  750. (volatile uint32_t *)dev_priv->status->handle;
  751. dev_priv->wait_fifo = savage_bci_wait_fifo_shadow;
  752. dev_priv->wait_evnt = savage_bci_wait_event_shadow;
  753. dev_priv->status_ptr[1023] = dev_priv->event_counter;
  754. } else {
  755. dev_priv->status_ptr = NULL;
  756. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  757. dev_priv->wait_fifo = savage_bci_wait_fifo_s3d;
  758. } else {
  759. dev_priv->wait_fifo = savage_bci_wait_fifo_s4;
  760. }
  761. dev_priv->wait_evnt = savage_bci_wait_event_reg;
  762. }
  763. /* cliprect functions */
  764. if (S3_SAVAGE3D_SERIES(dev_priv->chipset))
  765. dev_priv->emit_clip_rect = savage_emit_clip_rect_s3d;
  766. else
  767. dev_priv->emit_clip_rect = savage_emit_clip_rect_s4;
  768. if (savage_freelist_init(dev) < 0) {
  769. DRM_ERROR("could not initialize freelist\n");
  770. savage_do_cleanup_bci(dev);
  771. return -ENOMEM;
  772. }
  773. if (savage_dma_init(dev_priv) < 0) {
  774. DRM_ERROR("could not initialize command DMA\n");
  775. savage_do_cleanup_bci(dev);
  776. return -ENOMEM;
  777. }
  778. return 0;
  779. }
  780. static int savage_do_cleanup_bci(struct drm_device * dev)
  781. {
  782. drm_savage_private_t *dev_priv = dev->dev_private;
  783. if (dev_priv->cmd_dma == &dev_priv->fake_dma) {
  784. kfree(dev_priv->fake_dma.handle);
  785. } else if (dev_priv->cmd_dma && dev_priv->cmd_dma->handle &&
  786. dev_priv->cmd_dma->type == _DRM_AGP &&
  787. dev_priv->dma_type == SAVAGE_DMA_AGP)
  788. drm_legacy_ioremapfree(dev_priv->cmd_dma, dev);
  789. if (dev_priv->dma_type == SAVAGE_DMA_AGP &&
  790. dev->agp_buffer_map && dev->agp_buffer_map->handle) {
  791. drm_legacy_ioremapfree(dev->agp_buffer_map, dev);
  792. /* make sure the next instance (which may be running
  793. * in PCI mode) doesn't try to use an old
  794. * agp_buffer_map. */
  795. dev->agp_buffer_map = NULL;
  796. }
  797. kfree(dev_priv->dma_pages);
  798. return 0;
  799. }
  800. static int savage_bci_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
  801. {
  802. drm_savage_init_t *init = data;
  803. LOCK_TEST_WITH_RETURN(dev, file_priv);
  804. switch (init->func) {
  805. case SAVAGE_INIT_BCI:
  806. return savage_do_init_bci(dev, init);
  807. case SAVAGE_CLEANUP_BCI:
  808. return savage_do_cleanup_bci(dev);
  809. }
  810. return -EINVAL;
  811. }
  812. static int savage_bci_event_emit(struct drm_device *dev, void *data, struct drm_file *file_priv)
  813. {
  814. drm_savage_private_t *dev_priv = dev->dev_private;
  815. drm_savage_event_emit_t *event = data;
  816. DRM_DEBUG("\n");
  817. LOCK_TEST_WITH_RETURN(dev, file_priv);
  818. event->count = savage_bci_emit_event(dev_priv, event->flags);
  819. event->count |= dev_priv->event_wrap << 16;
  820. return 0;
  821. }
  822. static int savage_bci_event_wait(struct drm_device *dev, void *data, struct drm_file *file_priv)
  823. {
  824. drm_savage_private_t *dev_priv = dev->dev_private;
  825. drm_savage_event_wait_t *event = data;
  826. unsigned int event_e, hw_e;
  827. unsigned int event_w, hw_w;
  828. DRM_DEBUG("\n");
  829. UPDATE_EVENT_COUNTER();
  830. if (dev_priv->status_ptr)
  831. hw_e = dev_priv->status_ptr[1] & 0xffff;
  832. else
  833. hw_e = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
  834. hw_w = dev_priv->event_wrap;
  835. if (hw_e > dev_priv->event_counter)
  836. hw_w--; /* hardware hasn't passed the last wrap yet */
  837. event_e = event->count & 0xffff;
  838. event_w = event->count >> 16;
  839. /* Don't need to wait if
  840. * - event counter wrapped since the event was emitted or
  841. * - the hardware has advanced up to or over the event to wait for.
  842. */
  843. if (event_w < hw_w || (event_w == hw_w && event_e <= hw_e))
  844. return 0;
  845. else
  846. return dev_priv->wait_evnt(dev_priv, event_e);
  847. }
  848. /*
  849. * DMA buffer management
  850. */
  851. static int savage_bci_get_buffers(struct drm_device *dev,
  852. struct drm_file *file_priv,
  853. struct drm_dma *d)
  854. {
  855. struct drm_buf *buf;
  856. int i;
  857. for (i = d->granted_count; i < d->request_count; i++) {
  858. buf = savage_freelist_get(dev);
  859. if (!buf)
  860. return -EAGAIN;
  861. buf->file_priv = file_priv;
  862. if (copy_to_user(&d->request_indices[i],
  863. &buf->idx, sizeof(buf->idx)))
  864. return -EFAULT;
  865. if (copy_to_user(&d->request_sizes[i],
  866. &buf->total, sizeof(buf->total)))
  867. return -EFAULT;
  868. d->granted_count++;
  869. }
  870. return 0;
  871. }
  872. int savage_bci_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
  873. {
  874. struct drm_device_dma *dma = dev->dma;
  875. struct drm_dma *d = data;
  876. int ret = 0;
  877. LOCK_TEST_WITH_RETURN(dev, file_priv);
  878. /* Please don't send us buffers.
  879. */
  880. if (d->send_count != 0) {
  881. DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
  882. DRM_CURRENTPID, d->send_count);
  883. return -EINVAL;
  884. }
  885. /* We'll send you buffers.
  886. */
  887. if (d->request_count < 0 || d->request_count > dma->buf_count) {
  888. DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
  889. DRM_CURRENTPID, d->request_count, dma->buf_count);
  890. return -EINVAL;
  891. }
  892. d->granted_count = 0;
  893. if (d->request_count) {
  894. ret = savage_bci_get_buffers(dev, file_priv, d);
  895. }
  896. return ret;
  897. }
  898. void savage_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv)
  899. {
  900. struct drm_device_dma *dma = dev->dma;
  901. drm_savage_private_t *dev_priv = dev->dev_private;
  902. int release_idlelock = 0;
  903. int i;
  904. if (!dma)
  905. return;
  906. if (!dev_priv)
  907. return;
  908. if (!dma->buflist)
  909. return;
  910. if (file_priv->master && file_priv->master->lock.hw_lock) {
  911. drm_legacy_idlelock_take(&file_priv->master->lock);
  912. release_idlelock = 1;
  913. }
  914. for (i = 0; i < dma->buf_count; i++) {
  915. struct drm_buf *buf = dma->buflist[i];
  916. drm_savage_buf_priv_t *buf_priv = buf->dev_private;
  917. if (buf->file_priv == file_priv && buf_priv &&
  918. buf_priv->next == NULL && buf_priv->prev == NULL) {
  919. uint16_t event;
  920. DRM_DEBUG("reclaimed from client\n");
  921. event = savage_bci_emit_event(dev_priv, SAVAGE_WAIT_3D);
  922. SET_AGE(&buf_priv->age, event, dev_priv->event_wrap);
  923. savage_freelist_put(dev, buf);
  924. }
  925. }
  926. if (release_idlelock)
  927. drm_legacy_idlelock_release(&file_priv->master->lock);
  928. }
  929. const struct drm_ioctl_desc savage_ioctls[] = {
  930. DRM_IOCTL_DEF_DRV(SAVAGE_BCI_INIT, savage_bci_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  931. DRM_IOCTL_DEF_DRV(SAVAGE_BCI_CMDBUF, savage_bci_cmdbuf, DRM_AUTH),
  932. DRM_IOCTL_DEF_DRV(SAVAGE_BCI_EVENT_EMIT, savage_bci_event_emit, DRM_AUTH),
  933. DRM_IOCTL_DEF_DRV(SAVAGE_BCI_EVENT_WAIT, savage_bci_event_wait, DRM_AUTH),
  934. };
  935. int savage_max_ioctl = ARRAY_SIZE(savage_ioctls);