vmwgfx_cmdbuf.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /**************************************************************************
  2. *
  3. * Copyright © 2015 VMware, Inc., Palo Alto, CA., USA
  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
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include "ttm/ttm_bo_api.h"
  29. /*
  30. * Size of inline command buffers. Try to make sure that a page size is a
  31. * multiple of the DMA pool allocation size.
  32. */
  33. #define VMW_CMDBUF_INLINE_ALIGN 64
  34. #define VMW_CMDBUF_INLINE_SIZE \
  35. (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN))
  36. /**
  37. * struct vmw_cmdbuf_context - Command buffer context queues
  38. *
  39. * @submitted: List of command buffers that have been submitted to the
  40. * manager but not yet submitted to hardware.
  41. * @hw_submitted: List of command buffers submitted to hardware.
  42. * @preempted: List of preempted command buffers.
  43. * @num_hw_submitted: Number of buffers currently being processed by hardware
  44. */
  45. struct vmw_cmdbuf_context {
  46. struct list_head submitted;
  47. struct list_head hw_submitted;
  48. struct list_head preempted;
  49. unsigned num_hw_submitted;
  50. };
  51. /**
  52. * struct vmw_cmdbuf_man: - Command buffer manager
  53. *
  54. * @cur_mutex: Mutex protecting the command buffer used for incremental small
  55. * kernel command submissions, @cur.
  56. * @space_mutex: Mutex to protect against starvation when we allocate
  57. * main pool buffer space.
  58. * @work: A struct work_struct implementeing command buffer error handling.
  59. * Immutable.
  60. * @dev_priv: Pointer to the device private struct. Immutable.
  61. * @ctx: Array of command buffer context queues. The queues and the context
  62. * data is protected by @lock.
  63. * @error: List of command buffers that have caused device errors.
  64. * Protected by @lock.
  65. * @mm: Range manager for the command buffer space. Manager allocations and
  66. * frees are protected by @lock.
  67. * @cmd_space: Buffer object for the command buffer space, unless we were
  68. * able to make a contigous coherent DMA memory allocation, @handle. Immutable.
  69. * @map_obj: Mapping state for @cmd_space. Immutable.
  70. * @map: Pointer to command buffer space. May be a mapped buffer object or
  71. * a contigous coherent DMA memory allocation. Immutable.
  72. * @cur: Command buffer for small kernel command submissions. Protected by
  73. * the @cur_mutex.
  74. * @cur_pos: Space already used in @cur. Protected by @cur_mutex.
  75. * @default_size: Default size for the @cur command buffer. Immutable.
  76. * @max_hw_submitted: Max number of in-flight command buffers the device can
  77. * handle. Immutable.
  78. * @lock: Spinlock protecting command submission queues.
  79. * @header: Pool of DMA memory for device command buffer headers.
  80. * Internal protection.
  81. * @dheaders: Pool of DMA memory for device command buffer headers with trailing
  82. * space for inline data. Internal protection.
  83. * @tasklet: Tasklet struct for irq processing. Immutable.
  84. * @alloc_queue: Wait queue for processes waiting to allocate command buffer
  85. * space.
  86. * @idle_queue: Wait queue for processes waiting for command buffer idle.
  87. * @irq_on: Whether the process function has requested irq to be turned on.
  88. * Protected by @lock.
  89. * @using_mob: Whether the command buffer space is a MOB or a contigous DMA
  90. * allocation. Immutable.
  91. * @has_pool: Has a large pool of DMA memory which allows larger allocations.
  92. * Typically this is false only during bootstrap.
  93. * @handle: DMA address handle for the command buffer space if @using_mob is
  94. * false. Immutable.
  95. * @size: The size of the command buffer space. Immutable.
  96. */
  97. struct vmw_cmdbuf_man {
  98. struct mutex cur_mutex;
  99. struct mutex space_mutex;
  100. struct work_struct work;
  101. struct vmw_private *dev_priv;
  102. struct vmw_cmdbuf_context ctx[SVGA_CB_CONTEXT_MAX];
  103. struct list_head error;
  104. struct drm_mm mm;
  105. struct ttm_buffer_object *cmd_space;
  106. struct ttm_bo_kmap_obj map_obj;
  107. u8 *map;
  108. struct vmw_cmdbuf_header *cur;
  109. size_t cur_pos;
  110. size_t default_size;
  111. unsigned max_hw_submitted;
  112. spinlock_t lock;
  113. struct dma_pool *headers;
  114. struct dma_pool *dheaders;
  115. struct tasklet_struct tasklet;
  116. wait_queue_head_t alloc_queue;
  117. wait_queue_head_t idle_queue;
  118. bool irq_on;
  119. bool using_mob;
  120. bool has_pool;
  121. dma_addr_t handle;
  122. size_t size;
  123. };
  124. /**
  125. * struct vmw_cmdbuf_header - Command buffer metadata
  126. *
  127. * @man: The command buffer manager.
  128. * @cb_header: Device command buffer header, allocated from a DMA pool.
  129. * @cb_context: The device command buffer context.
  130. * @list: List head for attaching to the manager lists.
  131. * @node: The range manager node.
  132. * @handle. The DMA address of @cb_header. Handed to the device on command
  133. * buffer submission.
  134. * @cmd: Pointer to the command buffer space of this buffer.
  135. * @size: Size of the command buffer space of this buffer.
  136. * @reserved: Reserved space of this buffer.
  137. * @inline_space: Whether inline command buffer space is used.
  138. */
  139. struct vmw_cmdbuf_header {
  140. struct vmw_cmdbuf_man *man;
  141. SVGACBHeader *cb_header;
  142. SVGACBContext cb_context;
  143. struct list_head list;
  144. struct drm_mm_node node;
  145. dma_addr_t handle;
  146. u8 *cmd;
  147. size_t size;
  148. size_t reserved;
  149. bool inline_space;
  150. };
  151. /**
  152. * struct vmw_cmdbuf_dheader - Device command buffer header with inline
  153. * command buffer space.
  154. *
  155. * @cb_header: Device command buffer header.
  156. * @cmd: Inline command buffer space.
  157. */
  158. struct vmw_cmdbuf_dheader {
  159. SVGACBHeader cb_header;
  160. u8 cmd[VMW_CMDBUF_INLINE_SIZE] __aligned(VMW_CMDBUF_INLINE_ALIGN);
  161. };
  162. /**
  163. * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata
  164. *
  165. * @page_size: Size of requested command buffer space in pages.
  166. * @node: Pointer to the range manager node.
  167. * @done: True if this allocation has succeeded.
  168. */
  169. struct vmw_cmdbuf_alloc_info {
  170. size_t page_size;
  171. struct drm_mm_node *node;
  172. bool done;
  173. };
  174. /* Loop over each context in the command buffer manager. */
  175. #define for_each_cmdbuf_ctx(_man, _i, _ctx) \
  176. for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < SVGA_CB_CONTEXT_MAX; \
  177. ++(_i), ++(_ctx))
  178. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, bool enable);
  179. /**
  180. * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex.
  181. *
  182. * @man: The range manager.
  183. * @interruptible: Whether to wait interruptible when locking.
  184. */
  185. static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man *man, bool interruptible)
  186. {
  187. if (interruptible) {
  188. if (mutex_lock_interruptible(&man->cur_mutex))
  189. return -ERESTARTSYS;
  190. } else {
  191. mutex_lock(&man->cur_mutex);
  192. }
  193. return 0;
  194. }
  195. /**
  196. * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex.
  197. *
  198. * @man: The range manager.
  199. */
  200. static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man *man)
  201. {
  202. mutex_unlock(&man->cur_mutex);
  203. }
  204. /**
  205. * vmw_cmdbuf_header_inline_free - Free a struct vmw_cmdbuf_header that has
  206. * been used for the device context with inline command buffers.
  207. * Need not be called locked.
  208. *
  209. * @header: Pointer to the header to free.
  210. */
  211. static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header *header)
  212. {
  213. struct vmw_cmdbuf_dheader *dheader;
  214. if (WARN_ON_ONCE(!header->inline_space))
  215. return;
  216. dheader = container_of(header->cb_header, struct vmw_cmdbuf_dheader,
  217. cb_header);
  218. dma_pool_free(header->man->dheaders, dheader, header->handle);
  219. kfree(header);
  220. }
  221. /**
  222. * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
  223. * associated structures.
  224. *
  225. * header: Pointer to the header to free.
  226. *
  227. * For internal use. Must be called with man::lock held.
  228. */
  229. static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
  230. {
  231. struct vmw_cmdbuf_man *man = header->man;
  232. lockdep_assert_held_once(&man->lock);
  233. if (header->inline_space) {
  234. vmw_cmdbuf_header_inline_free(header);
  235. return;
  236. }
  237. drm_mm_remove_node(&header->node);
  238. wake_up_all(&man->alloc_queue);
  239. if (header->cb_header)
  240. dma_pool_free(man->headers, header->cb_header,
  241. header->handle);
  242. kfree(header);
  243. }
  244. /**
  245. * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
  246. * associated structures.
  247. *
  248. * @header: Pointer to the header to free.
  249. */
  250. void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
  251. {
  252. struct vmw_cmdbuf_man *man = header->man;
  253. /* Avoid locking if inline_space */
  254. if (header->inline_space) {
  255. vmw_cmdbuf_header_inline_free(header);
  256. return;
  257. }
  258. spin_lock_bh(&man->lock);
  259. __vmw_cmdbuf_header_free(header);
  260. spin_unlock_bh(&man->lock);
  261. }
  262. /**
  263. * vmw_cmbuf_header_submit: Submit a command buffer to hardware.
  264. *
  265. * @header: The header of the buffer to submit.
  266. */
  267. static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header *header)
  268. {
  269. struct vmw_cmdbuf_man *man = header->man;
  270. u32 val;
  271. val = upper_32_bits(header->handle);
  272. vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val);
  273. val = lower_32_bits(header->handle);
  274. val |= header->cb_context & SVGA_CB_CONTEXT_MASK;
  275. vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val);
  276. return header->cb_header->status;
  277. }
  278. /**
  279. * vmw_cmdbuf_ctx_init: Initialize a command buffer context.
  280. *
  281. * @ctx: The command buffer context to initialize
  282. */
  283. static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx)
  284. {
  285. INIT_LIST_HEAD(&ctx->hw_submitted);
  286. INIT_LIST_HEAD(&ctx->submitted);
  287. INIT_LIST_HEAD(&ctx->preempted);
  288. ctx->num_hw_submitted = 0;
  289. }
  290. /**
  291. * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer
  292. * context.
  293. *
  294. * @man: The command buffer manager.
  295. * @ctx: The command buffer context.
  296. *
  297. * Submits command buffers to hardware until there are no more command
  298. * buffers to submit or the hardware can't handle more command buffers.
  299. */
  300. static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man,
  301. struct vmw_cmdbuf_context *ctx)
  302. {
  303. while (ctx->num_hw_submitted < man->max_hw_submitted &&
  304. !list_empty(&ctx->submitted)) {
  305. struct vmw_cmdbuf_header *entry;
  306. SVGACBStatus status;
  307. entry = list_first_entry(&ctx->submitted,
  308. struct vmw_cmdbuf_header,
  309. list);
  310. status = vmw_cmdbuf_header_submit(entry);
  311. /* This should never happen */
  312. if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) {
  313. entry->cb_header->status = SVGA_CB_STATUS_NONE;
  314. break;
  315. }
  316. list_del(&entry->list);
  317. list_add_tail(&entry->list, &ctx->hw_submitted);
  318. ctx->num_hw_submitted++;
  319. }
  320. }
  321. /**
  322. * vmw_cmdbuf_ctx_submit: Process a command buffer context.
  323. *
  324. * @man: The command buffer manager.
  325. * @ctx: The command buffer context.
  326. *
  327. * Submit command buffers to hardware if possible, and process finished
  328. * buffers. Typically freeing them, but on preemption or error take
  329. * appropriate action. Wake up waiters if appropriate.
  330. */
  331. static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man,
  332. struct vmw_cmdbuf_context *ctx,
  333. int *notempty)
  334. {
  335. struct vmw_cmdbuf_header *entry, *next;
  336. vmw_cmdbuf_ctx_submit(man, ctx);
  337. list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) {
  338. SVGACBStatus status = entry->cb_header->status;
  339. if (status == SVGA_CB_STATUS_NONE)
  340. break;
  341. list_del(&entry->list);
  342. wake_up_all(&man->idle_queue);
  343. ctx->num_hw_submitted--;
  344. switch (status) {
  345. case SVGA_CB_STATUS_COMPLETED:
  346. __vmw_cmdbuf_header_free(entry);
  347. break;
  348. case SVGA_CB_STATUS_COMMAND_ERROR:
  349. case SVGA_CB_STATUS_CB_HEADER_ERROR:
  350. list_add_tail(&entry->list, &man->error);
  351. schedule_work(&man->work);
  352. break;
  353. case SVGA_CB_STATUS_PREEMPTED:
  354. list_add(&entry->list, &ctx->preempted);
  355. break;
  356. default:
  357. WARN_ONCE(true, "Undefined command buffer status.\n");
  358. __vmw_cmdbuf_header_free(entry);
  359. break;
  360. }
  361. }
  362. vmw_cmdbuf_ctx_submit(man, ctx);
  363. if (!list_empty(&ctx->submitted))
  364. (*notempty)++;
  365. }
  366. /**
  367. * vmw_cmdbuf_man_process - Process all command buffer contexts and
  368. * switch on and off irqs as appropriate.
  369. *
  370. * @man: The command buffer manager.
  371. *
  372. * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has
  373. * command buffers left that are not submitted to hardware, Make sure
  374. * IRQ handling is turned on. Otherwise, make sure it's turned off.
  375. */
  376. static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man)
  377. {
  378. int notempty;
  379. struct vmw_cmdbuf_context *ctx;
  380. int i;
  381. retry:
  382. notempty = 0;
  383. for_each_cmdbuf_ctx(man, i, ctx)
  384. vmw_cmdbuf_ctx_process(man, ctx, &notempty);
  385. if (man->irq_on && !notempty) {
  386. vmw_generic_waiter_remove(man->dev_priv,
  387. SVGA_IRQFLAG_COMMAND_BUFFER,
  388. &man->dev_priv->cmdbuf_waiters);
  389. man->irq_on = false;
  390. } else if (!man->irq_on && notempty) {
  391. vmw_generic_waiter_add(man->dev_priv,
  392. SVGA_IRQFLAG_COMMAND_BUFFER,
  393. &man->dev_priv->cmdbuf_waiters);
  394. man->irq_on = true;
  395. /* Rerun in case we just missed an irq. */
  396. goto retry;
  397. }
  398. }
  399. /**
  400. * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a
  401. * command buffer context
  402. *
  403. * @man: The command buffer manager.
  404. * @header: The header of the buffer to submit.
  405. * @cb_context: The command buffer context to use.
  406. *
  407. * This function adds @header to the "submitted" queue of the command
  408. * buffer context identified by @cb_context. It then calls the command buffer
  409. * manager processing to potentially submit the buffer to hardware.
  410. * @man->lock needs to be held when calling this function.
  411. */
  412. static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man,
  413. struct vmw_cmdbuf_header *header,
  414. SVGACBContext cb_context)
  415. {
  416. if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT))
  417. header->cb_header->dxContext = 0;
  418. header->cb_context = cb_context;
  419. list_add_tail(&header->list, &man->ctx[cb_context].submitted);
  420. vmw_cmdbuf_man_process(man);
  421. }
  422. /**
  423. * vmw_cmdbuf_man_tasklet - The main part of the command buffer interrupt
  424. * handler implemented as a tasklet.
  425. *
  426. * @data: Tasklet closure. A pointer to the command buffer manager cast to
  427. * an unsigned long.
  428. *
  429. * The bottom half (tasklet) of the interrupt handler simply calls into the
  430. * command buffer processor to free finished buffers and submit any
  431. * queued buffers to hardware.
  432. */
  433. static void vmw_cmdbuf_man_tasklet(unsigned long data)
  434. {
  435. struct vmw_cmdbuf_man *man = (struct vmw_cmdbuf_man *) data;
  436. spin_lock(&man->lock);
  437. vmw_cmdbuf_man_process(man);
  438. spin_unlock(&man->lock);
  439. }
  440. /**
  441. * vmw_cmdbuf_work_func - The deferred work function that handles
  442. * command buffer errors.
  443. *
  444. * @work: The work func closure argument.
  445. *
  446. * Restarting the command buffer context after an error requires process
  447. * context, so it is deferred to this work function.
  448. */
  449. static void vmw_cmdbuf_work_func(struct work_struct *work)
  450. {
  451. struct vmw_cmdbuf_man *man =
  452. container_of(work, struct vmw_cmdbuf_man, work);
  453. struct vmw_cmdbuf_header *entry, *next;
  454. uint32_t dummy;
  455. bool restart = false;
  456. spin_lock_bh(&man->lock);
  457. list_for_each_entry_safe(entry, next, &man->error, list) {
  458. restart = true;
  459. DRM_ERROR("Command buffer error.\n");
  460. list_del(&entry->list);
  461. __vmw_cmdbuf_header_free(entry);
  462. wake_up_all(&man->idle_queue);
  463. }
  464. spin_unlock_bh(&man->lock);
  465. if (restart && vmw_cmdbuf_startstop(man, true))
  466. DRM_ERROR("Failed restarting command buffer context 0.\n");
  467. /* Send a new fence in case one was removed */
  468. vmw_fifo_send_fence(man->dev_priv, &dummy);
  469. }
  470. /**
  471. * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle.
  472. *
  473. * @man: The command buffer manager.
  474. * @check_preempted: Check also the preempted queue for pending command buffers.
  475. *
  476. */
  477. static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man,
  478. bool check_preempted)
  479. {
  480. struct vmw_cmdbuf_context *ctx;
  481. bool idle = false;
  482. int i;
  483. spin_lock_bh(&man->lock);
  484. vmw_cmdbuf_man_process(man);
  485. for_each_cmdbuf_ctx(man, i, ctx) {
  486. if (!list_empty(&ctx->submitted) ||
  487. !list_empty(&ctx->hw_submitted) ||
  488. (check_preempted && !list_empty(&ctx->preempted)))
  489. goto out_unlock;
  490. }
  491. idle = list_empty(&man->error);
  492. out_unlock:
  493. spin_unlock_bh(&man->lock);
  494. return idle;
  495. }
  496. /**
  497. * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  498. * command submissions
  499. *
  500. * @man: The command buffer manager.
  501. *
  502. * Flushes the current command buffer without allocating a new one. A new one
  503. * is automatically allocated when needed. Call with @man->cur_mutex held.
  504. */
  505. static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man)
  506. {
  507. struct vmw_cmdbuf_header *cur = man->cur;
  508. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  509. if (!cur)
  510. return;
  511. spin_lock_bh(&man->lock);
  512. if (man->cur_pos == 0) {
  513. __vmw_cmdbuf_header_free(cur);
  514. goto out_unlock;
  515. }
  516. man->cur->cb_header->length = man->cur_pos;
  517. vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0);
  518. out_unlock:
  519. spin_unlock_bh(&man->lock);
  520. man->cur = NULL;
  521. man->cur_pos = 0;
  522. }
  523. /**
  524. * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  525. * command submissions
  526. *
  527. * @man: The command buffer manager.
  528. * @interruptible: Whether to sleep interruptible when sleeping.
  529. *
  530. * Flushes the current command buffer without allocating a new one. A new one
  531. * is automatically allocated when needed.
  532. */
  533. int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
  534. bool interruptible)
  535. {
  536. int ret = vmw_cmdbuf_cur_lock(man, interruptible);
  537. if (ret)
  538. return ret;
  539. __vmw_cmdbuf_cur_flush(man);
  540. vmw_cmdbuf_cur_unlock(man);
  541. return 0;
  542. }
  543. /**
  544. * vmw_cmdbuf_idle - Wait for command buffer manager idle.
  545. *
  546. * @man: The command buffer manager.
  547. * @interruptible: Sleep interruptible while waiting.
  548. * @timeout: Time out after this many ticks.
  549. *
  550. * Wait until the command buffer manager has processed all command buffers,
  551. * or until a timeout occurs. If a timeout occurs, the function will return
  552. * -EBUSY.
  553. */
  554. int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
  555. unsigned long timeout)
  556. {
  557. int ret;
  558. ret = vmw_cmdbuf_cur_flush(man, interruptible);
  559. vmw_generic_waiter_add(man->dev_priv,
  560. SVGA_IRQFLAG_COMMAND_BUFFER,
  561. &man->dev_priv->cmdbuf_waiters);
  562. if (interruptible) {
  563. ret = wait_event_interruptible_timeout
  564. (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  565. timeout);
  566. } else {
  567. ret = wait_event_timeout
  568. (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  569. timeout);
  570. }
  571. vmw_generic_waiter_remove(man->dev_priv,
  572. SVGA_IRQFLAG_COMMAND_BUFFER,
  573. &man->dev_priv->cmdbuf_waiters);
  574. if (ret == 0) {
  575. if (!vmw_cmdbuf_man_idle(man, true))
  576. ret = -EBUSY;
  577. else
  578. ret = 0;
  579. }
  580. if (ret > 0)
  581. ret = 0;
  582. return ret;
  583. }
  584. /**
  585. * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool.
  586. *
  587. * @man: The command buffer manager.
  588. * @info: Allocation info. Will hold the size on entry and allocated mm node
  589. * on successful return.
  590. *
  591. * Try to allocate buffer space from the main pool. Returns true if succeeded.
  592. * If a fatal error was hit, the error code is returned in @info->ret.
  593. */
  594. static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man,
  595. struct vmw_cmdbuf_alloc_info *info)
  596. {
  597. int ret;
  598. if (info->done)
  599. return true;
  600. memset(info->node, 0, sizeof(*info->node));
  601. spin_lock_bh(&man->lock);
  602. ret = drm_mm_insert_node_generic(&man->mm, info->node, info->page_size,
  603. 0, 0,
  604. DRM_MM_SEARCH_DEFAULT,
  605. DRM_MM_CREATE_DEFAULT);
  606. if (ret) {
  607. vmw_cmdbuf_man_process(man);
  608. ret = drm_mm_insert_node_generic(&man->mm, info->node,
  609. info->page_size, 0, 0,
  610. DRM_MM_SEARCH_DEFAULT,
  611. DRM_MM_CREATE_DEFAULT);
  612. }
  613. spin_unlock_bh(&man->lock);
  614. info->done = !ret;
  615. return info->done;
  616. }
  617. /**
  618. * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool.
  619. *
  620. * @man: The command buffer manager.
  621. * @node: Pointer to pre-allocated range-manager node.
  622. * @size: The size of the allocation.
  623. * @interruptible: Whether to sleep interruptible while waiting for space.
  624. *
  625. * This function allocates buffer space from the main pool, and if there is
  626. * no space available ATM, it turns on IRQ handling and sleeps waiting for it to
  627. * become available.
  628. */
  629. static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man,
  630. struct drm_mm_node *node,
  631. size_t size,
  632. bool interruptible)
  633. {
  634. struct vmw_cmdbuf_alloc_info info;
  635. info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT;
  636. info.node = node;
  637. info.done = false;
  638. /*
  639. * To prevent starvation of large requests, only one allocating call
  640. * at a time waiting for space.
  641. */
  642. if (interruptible) {
  643. if (mutex_lock_interruptible(&man->space_mutex))
  644. return -ERESTARTSYS;
  645. } else {
  646. mutex_lock(&man->space_mutex);
  647. }
  648. /* Try to allocate space without waiting. */
  649. if (vmw_cmdbuf_try_alloc(man, &info))
  650. goto out_unlock;
  651. vmw_generic_waiter_add(man->dev_priv,
  652. SVGA_IRQFLAG_COMMAND_BUFFER,
  653. &man->dev_priv->cmdbuf_waiters);
  654. if (interruptible) {
  655. int ret;
  656. ret = wait_event_interruptible
  657. (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  658. if (ret) {
  659. vmw_generic_waiter_remove
  660. (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER,
  661. &man->dev_priv->cmdbuf_waiters);
  662. mutex_unlock(&man->space_mutex);
  663. return ret;
  664. }
  665. } else {
  666. wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  667. }
  668. vmw_generic_waiter_remove(man->dev_priv,
  669. SVGA_IRQFLAG_COMMAND_BUFFER,
  670. &man->dev_priv->cmdbuf_waiters);
  671. out_unlock:
  672. mutex_unlock(&man->space_mutex);
  673. return 0;
  674. }
  675. /**
  676. * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer
  677. * space from the main pool.
  678. *
  679. * @man: The command buffer manager.
  680. * @header: Pointer to the header to set up.
  681. * @size: The requested size of the buffer space.
  682. * @interruptible: Whether to sleep interruptible while waiting for space.
  683. */
  684. static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man,
  685. struct vmw_cmdbuf_header *header,
  686. size_t size,
  687. bool interruptible)
  688. {
  689. SVGACBHeader *cb_hdr;
  690. size_t offset;
  691. int ret;
  692. if (!man->has_pool)
  693. return -ENOMEM;
  694. ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible);
  695. if (ret)
  696. return ret;
  697. header->cb_header = dma_pool_alloc(man->headers, GFP_KERNEL,
  698. &header->handle);
  699. if (!header->cb_header) {
  700. ret = -ENOMEM;
  701. goto out_no_cb_header;
  702. }
  703. header->size = header->node.size << PAGE_SHIFT;
  704. cb_hdr = header->cb_header;
  705. offset = header->node.start << PAGE_SHIFT;
  706. header->cmd = man->map + offset;
  707. memset(cb_hdr, 0, sizeof(*cb_hdr));
  708. if (man->using_mob) {
  709. cb_hdr->flags = SVGA_CB_FLAG_MOB;
  710. cb_hdr->ptr.mob.mobid = man->cmd_space->mem.start;
  711. cb_hdr->ptr.mob.mobOffset = offset;
  712. } else {
  713. cb_hdr->ptr.pa = (u64)man->handle + (u64)offset;
  714. }
  715. return 0;
  716. out_no_cb_header:
  717. spin_lock_bh(&man->lock);
  718. drm_mm_remove_node(&header->node);
  719. spin_unlock_bh(&man->lock);
  720. return ret;
  721. }
  722. /**
  723. * vmw_cmdbuf_space_inline - Set up a command buffer header with
  724. * inline command buffer space.
  725. *
  726. * @man: The command buffer manager.
  727. * @header: Pointer to the header to set up.
  728. * @size: The requested size of the buffer space.
  729. */
  730. static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man,
  731. struct vmw_cmdbuf_header *header,
  732. int size)
  733. {
  734. struct vmw_cmdbuf_dheader *dheader;
  735. SVGACBHeader *cb_hdr;
  736. if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE))
  737. return -ENOMEM;
  738. dheader = dma_pool_alloc(man->dheaders, GFP_KERNEL,
  739. &header->handle);
  740. if (!dheader)
  741. return -ENOMEM;
  742. header->inline_space = true;
  743. header->size = VMW_CMDBUF_INLINE_SIZE;
  744. cb_hdr = &dheader->cb_header;
  745. header->cb_header = cb_hdr;
  746. header->cmd = dheader->cmd;
  747. memset(dheader, 0, sizeof(*dheader));
  748. cb_hdr->status = SVGA_CB_STATUS_NONE;
  749. cb_hdr->flags = SVGA_CB_FLAG_NONE;
  750. cb_hdr->ptr.pa = (u64)header->handle +
  751. (u64)offsetof(struct vmw_cmdbuf_dheader, cmd);
  752. return 0;
  753. }
  754. /**
  755. * vmw_cmdbuf_alloc - Allocate a command buffer header complete with
  756. * command buffer space.
  757. *
  758. * @man: The command buffer manager.
  759. * @size: The requested size of the buffer space.
  760. * @interruptible: Whether to sleep interruptible while waiting for space.
  761. * @p_header: points to a header pointer to populate on successful return.
  762. *
  763. * Returns a pointer to command buffer space if successful. Otherwise
  764. * returns an error pointer. The header pointer returned in @p_header should
  765. * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit().
  766. */
  767. void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
  768. size_t size, bool interruptible,
  769. struct vmw_cmdbuf_header **p_header)
  770. {
  771. struct vmw_cmdbuf_header *header;
  772. int ret = 0;
  773. *p_header = NULL;
  774. header = kzalloc(sizeof(*header), GFP_KERNEL);
  775. if (!header)
  776. return ERR_PTR(-ENOMEM);
  777. if (size <= VMW_CMDBUF_INLINE_SIZE)
  778. ret = vmw_cmdbuf_space_inline(man, header, size);
  779. else
  780. ret = vmw_cmdbuf_space_pool(man, header, size, interruptible);
  781. if (ret) {
  782. kfree(header);
  783. return ERR_PTR(ret);
  784. }
  785. header->man = man;
  786. INIT_LIST_HEAD(&header->list);
  787. header->cb_header->status = SVGA_CB_STATUS_NONE;
  788. *p_header = header;
  789. return header->cmd;
  790. }
  791. /**
  792. * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current
  793. * command buffer.
  794. *
  795. * @man: The command buffer manager.
  796. * @size: The requested size of the commands.
  797. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  798. * @interruptible: Whether to sleep interruptible while waiting for space.
  799. *
  800. * Returns a pointer to command buffer space if successful. Otherwise
  801. * returns an error pointer.
  802. */
  803. static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man,
  804. size_t size,
  805. int ctx_id,
  806. bool interruptible)
  807. {
  808. struct vmw_cmdbuf_header *cur;
  809. void *ret;
  810. if (vmw_cmdbuf_cur_lock(man, interruptible))
  811. return ERR_PTR(-ERESTARTSYS);
  812. cur = man->cur;
  813. if (cur && (size + man->cur_pos > cur->size ||
  814. ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) &&
  815. ctx_id != cur->cb_header->dxContext)))
  816. __vmw_cmdbuf_cur_flush(man);
  817. if (!man->cur) {
  818. ret = vmw_cmdbuf_alloc(man,
  819. max_t(size_t, size, man->default_size),
  820. interruptible, &man->cur);
  821. if (IS_ERR(ret)) {
  822. vmw_cmdbuf_cur_unlock(man);
  823. return ret;
  824. }
  825. cur = man->cur;
  826. }
  827. if (ctx_id != SVGA3D_INVALID_ID) {
  828. cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  829. cur->cb_header->dxContext = ctx_id;
  830. }
  831. cur->reserved = size;
  832. return (void *) (man->cur->cmd + man->cur_pos);
  833. }
  834. /**
  835. * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
  836. *
  837. * @man: The command buffer manager.
  838. * @size: The size of the commands actually written.
  839. * @flush: Whether to flush the command buffer immediately.
  840. */
  841. static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man,
  842. size_t size, bool flush)
  843. {
  844. struct vmw_cmdbuf_header *cur = man->cur;
  845. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  846. WARN_ON(size > cur->reserved);
  847. man->cur_pos += size;
  848. if (!size)
  849. cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  850. if (flush)
  851. __vmw_cmdbuf_cur_flush(man);
  852. vmw_cmdbuf_cur_unlock(man);
  853. }
  854. /**
  855. * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
  856. *
  857. * @man: The command buffer manager.
  858. * @size: The requested size of the commands.
  859. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  860. * @interruptible: Whether to sleep interruptible while waiting for space.
  861. * @header: Header of the command buffer. NULL if the current command buffer
  862. * should be used.
  863. *
  864. * Returns a pointer to command buffer space if successful. Otherwise
  865. * returns an error pointer.
  866. */
  867. void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
  868. int ctx_id, bool interruptible,
  869. struct vmw_cmdbuf_header *header)
  870. {
  871. if (!header)
  872. return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible);
  873. if (size > header->size)
  874. return ERR_PTR(-EINVAL);
  875. if (ctx_id != SVGA3D_INVALID_ID) {
  876. header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  877. header->cb_header->dxContext = ctx_id;
  878. }
  879. header->reserved = size;
  880. return header->cmd;
  881. }
  882. /**
  883. * vmw_cmdbuf_commit - Commit commands in a command buffer.
  884. *
  885. * @man: The command buffer manager.
  886. * @size: The size of the commands actually written.
  887. * @header: Header of the command buffer. NULL if the current command buffer
  888. * should be used.
  889. * @flush: Whether to flush the command buffer immediately.
  890. */
  891. void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
  892. struct vmw_cmdbuf_header *header, bool flush)
  893. {
  894. if (!header) {
  895. vmw_cmdbuf_commit_cur(man, size, flush);
  896. return;
  897. }
  898. (void) vmw_cmdbuf_cur_lock(man, false);
  899. __vmw_cmdbuf_cur_flush(man);
  900. WARN_ON(size > header->reserved);
  901. man->cur = header;
  902. man->cur_pos = size;
  903. if (!size)
  904. header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  905. if (flush)
  906. __vmw_cmdbuf_cur_flush(man);
  907. vmw_cmdbuf_cur_unlock(man);
  908. }
  909. /**
  910. * vmw_cmdbuf_tasklet_schedule - Schedule the interrupt handler bottom half.
  911. *
  912. * @man: The command buffer manager.
  913. */
  914. void vmw_cmdbuf_tasklet_schedule(struct vmw_cmdbuf_man *man)
  915. {
  916. if (!man)
  917. return;
  918. tasklet_schedule(&man->tasklet);
  919. }
  920. /**
  921. * vmw_cmdbuf_send_device_command - Send a command through the device context.
  922. *
  923. * @man: The command buffer manager.
  924. * @command: Pointer to the command to send.
  925. * @size: Size of the command.
  926. *
  927. * Synchronously sends a device context command.
  928. */
  929. static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man,
  930. const void *command,
  931. size_t size)
  932. {
  933. struct vmw_cmdbuf_header *header;
  934. int status;
  935. void *cmd = vmw_cmdbuf_alloc(man, size, false, &header);
  936. if (IS_ERR(cmd))
  937. return PTR_ERR(cmd);
  938. memcpy(cmd, command, size);
  939. header->cb_header->length = size;
  940. header->cb_context = SVGA_CB_CONTEXT_DEVICE;
  941. spin_lock_bh(&man->lock);
  942. status = vmw_cmdbuf_header_submit(header);
  943. spin_unlock_bh(&man->lock);
  944. vmw_cmdbuf_header_free(header);
  945. if (status != SVGA_CB_STATUS_COMPLETED) {
  946. DRM_ERROR("Device context command failed with status %d\n",
  947. status);
  948. return -EINVAL;
  949. }
  950. return 0;
  951. }
  952. /**
  953. * vmw_cmdbuf_startstop - Send a start / stop command through the device
  954. * context.
  955. *
  956. * @man: The command buffer manager.
  957. * @enable: Whether to enable or disable the context.
  958. *
  959. * Synchronously sends a device start / stop context command.
  960. */
  961. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man,
  962. bool enable)
  963. {
  964. struct {
  965. uint32 id;
  966. SVGADCCmdStartStop body;
  967. } __packed cmd;
  968. cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT;
  969. cmd.body.enable = (enable) ? 1 : 0;
  970. cmd.body.context = SVGA_CB_CONTEXT_0;
  971. return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
  972. }
  973. /**
  974. * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
  975. *
  976. * @man: The command buffer manager.
  977. * @size: The size of the main space pool.
  978. * @default_size: The default size of the command buffer for small kernel
  979. * submissions.
  980. *
  981. * Set the size and allocate the main command buffer space pool,
  982. * as well as the default size of the command buffer for
  983. * small kernel submissions. If successful, this enables large command
  984. * submissions. Note that this function requires that rudimentary command
  985. * submission is already available and that the MOB memory manager is alive.
  986. * Returns 0 on success. Negative error code on failure.
  987. */
  988. int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man,
  989. size_t size, size_t default_size)
  990. {
  991. struct vmw_private *dev_priv = man->dev_priv;
  992. bool dummy;
  993. int ret;
  994. if (man->has_pool)
  995. return -EINVAL;
  996. /* First, try to allocate a huge chunk of DMA memory */
  997. size = PAGE_ALIGN(size);
  998. man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size,
  999. &man->handle, GFP_KERNEL);
  1000. if (man->map) {
  1001. man->using_mob = false;
  1002. } else {
  1003. /*
  1004. * DMA memory failed. If we can have command buffers in a
  1005. * MOB, try to use that instead. Note that this will
  1006. * actually call into the already enabled manager, when
  1007. * binding the MOB.
  1008. */
  1009. if (!(dev_priv->capabilities & SVGA_CAP_DX))
  1010. return -ENOMEM;
  1011. ret = ttm_bo_create(&dev_priv->bdev, size, ttm_bo_type_device,
  1012. &vmw_mob_ne_placement, 0, false, NULL,
  1013. &man->cmd_space);
  1014. if (ret)
  1015. return ret;
  1016. man->using_mob = true;
  1017. ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT,
  1018. &man->map_obj);
  1019. if (ret)
  1020. goto out_no_map;
  1021. man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy);
  1022. }
  1023. man->size = size;
  1024. drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT);
  1025. man->has_pool = true;
  1026. /*
  1027. * For now, set the default size to VMW_CMDBUF_INLINE_SIZE to
  1028. * prevent deadlocks from happening when vmw_cmdbuf_space_pool()
  1029. * needs to wait for space and we block on further command
  1030. * submissions to be able to free up space.
  1031. */
  1032. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1033. DRM_INFO("Using command buffers with %s pool.\n",
  1034. (man->using_mob) ? "MOB" : "DMA");
  1035. return 0;
  1036. out_no_map:
  1037. if (man->using_mob)
  1038. ttm_bo_unref(&man->cmd_space);
  1039. return ret;
  1040. }
  1041. /**
  1042. * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
  1043. * inline command buffer submissions only.
  1044. *
  1045. * @dev_priv: Pointer to device private structure.
  1046. *
  1047. * Returns a pointer to a cummand buffer manager to success or error pointer
  1048. * on failure. The command buffer manager will be enabled for submissions of
  1049. * size VMW_CMDBUF_INLINE_SIZE only.
  1050. */
  1051. struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv)
  1052. {
  1053. struct vmw_cmdbuf_man *man;
  1054. struct vmw_cmdbuf_context *ctx;
  1055. int i;
  1056. int ret;
  1057. if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS))
  1058. return ERR_PTR(-ENOSYS);
  1059. man = kzalloc(sizeof(*man), GFP_KERNEL);
  1060. if (!man)
  1061. return ERR_PTR(-ENOMEM);
  1062. man->headers = dma_pool_create("vmwgfx cmdbuf",
  1063. &dev_priv->dev->pdev->dev,
  1064. sizeof(SVGACBHeader),
  1065. 64, PAGE_SIZE);
  1066. if (!man->headers) {
  1067. ret = -ENOMEM;
  1068. goto out_no_pool;
  1069. }
  1070. man->dheaders = dma_pool_create("vmwgfx inline cmdbuf",
  1071. &dev_priv->dev->pdev->dev,
  1072. sizeof(struct vmw_cmdbuf_dheader),
  1073. 64, PAGE_SIZE);
  1074. if (!man->dheaders) {
  1075. ret = -ENOMEM;
  1076. goto out_no_dpool;
  1077. }
  1078. for_each_cmdbuf_ctx(man, i, ctx)
  1079. vmw_cmdbuf_ctx_init(ctx);
  1080. INIT_LIST_HEAD(&man->error);
  1081. spin_lock_init(&man->lock);
  1082. mutex_init(&man->cur_mutex);
  1083. mutex_init(&man->space_mutex);
  1084. tasklet_init(&man->tasklet, vmw_cmdbuf_man_tasklet,
  1085. (unsigned long) man);
  1086. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1087. init_waitqueue_head(&man->alloc_queue);
  1088. init_waitqueue_head(&man->idle_queue);
  1089. man->dev_priv = dev_priv;
  1090. man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1;
  1091. INIT_WORK(&man->work, &vmw_cmdbuf_work_func);
  1092. vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR,
  1093. &dev_priv->error_waiters);
  1094. ret = vmw_cmdbuf_startstop(man, true);
  1095. if (ret) {
  1096. DRM_ERROR("Failed starting command buffer context 0.\n");
  1097. vmw_cmdbuf_man_destroy(man);
  1098. return ERR_PTR(ret);
  1099. }
  1100. return man;
  1101. out_no_dpool:
  1102. dma_pool_destroy(man->headers);
  1103. out_no_pool:
  1104. kfree(man);
  1105. return ERR_PTR(ret);
  1106. }
  1107. /**
  1108. * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
  1109. *
  1110. * @man: Pointer to a command buffer manager.
  1111. *
  1112. * This function removes the main buffer space pool, and should be called
  1113. * before MOB memory management is removed. When this function has been called,
  1114. * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
  1115. * less are allowed, and the default size of the command buffer for small kernel
  1116. * submissions is also set to this size.
  1117. */
  1118. void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man)
  1119. {
  1120. if (!man->has_pool)
  1121. return;
  1122. man->has_pool = false;
  1123. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1124. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1125. if (man->using_mob) {
  1126. (void) ttm_bo_kunmap(&man->map_obj);
  1127. ttm_bo_unref(&man->cmd_space);
  1128. } else {
  1129. dma_free_coherent(&man->dev_priv->dev->pdev->dev,
  1130. man->size, man->map, man->handle);
  1131. }
  1132. }
  1133. /**
  1134. * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
  1135. *
  1136. * @man: Pointer to a command buffer manager.
  1137. *
  1138. * This function idles and then destroys a command buffer manager.
  1139. */
  1140. void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man)
  1141. {
  1142. WARN_ON_ONCE(man->has_pool);
  1143. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1144. if (vmw_cmdbuf_startstop(man, false))
  1145. DRM_ERROR("Failed stopping command buffer context 0.\n");
  1146. vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR,
  1147. &man->dev_priv->error_waiters);
  1148. tasklet_kill(&man->tasklet);
  1149. (void) cancel_work_sync(&man->work);
  1150. dma_pool_destroy(man->dheaders);
  1151. dma_pool_destroy(man->headers);
  1152. mutex_destroy(&man->cur_mutex);
  1153. mutex_destroy(&man->space_mutex);
  1154. kfree(man);
  1155. }