vmwgfx_cmdbuf.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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. BUG_ON(!spin_is_locked(&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. if (sizeof(header->handle) > 4)
  272. val = (header->handle >> 32);
  273. else
  274. val = 0;
  275. vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val);
  276. val = (header->handle & 0xFFFFFFFFULL);
  277. val |= header->cb_context & SVGA_CB_CONTEXT_MASK;
  278. vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val);
  279. return header->cb_header->status;
  280. }
  281. /**
  282. * vmw_cmdbuf_ctx_init: Initialize a command buffer context.
  283. *
  284. * @ctx: The command buffer context to initialize
  285. */
  286. static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx)
  287. {
  288. INIT_LIST_HEAD(&ctx->hw_submitted);
  289. INIT_LIST_HEAD(&ctx->submitted);
  290. INIT_LIST_HEAD(&ctx->preempted);
  291. ctx->num_hw_submitted = 0;
  292. }
  293. /**
  294. * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer
  295. * context.
  296. *
  297. * @man: The command buffer manager.
  298. * @ctx: The command buffer context.
  299. *
  300. * Submits command buffers to hardware until there are no more command
  301. * buffers to submit or the hardware can't handle more command buffers.
  302. */
  303. static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man,
  304. struct vmw_cmdbuf_context *ctx)
  305. {
  306. while (ctx->num_hw_submitted < man->max_hw_submitted &&
  307. !list_empty(&ctx->submitted)) {
  308. struct vmw_cmdbuf_header *entry;
  309. SVGACBStatus status;
  310. entry = list_first_entry(&ctx->submitted,
  311. struct vmw_cmdbuf_header,
  312. list);
  313. status = vmw_cmdbuf_header_submit(entry);
  314. /* This should never happen */
  315. if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) {
  316. entry->cb_header->status = SVGA_CB_STATUS_NONE;
  317. break;
  318. }
  319. list_del(&entry->list);
  320. list_add_tail(&entry->list, &ctx->hw_submitted);
  321. ctx->num_hw_submitted++;
  322. }
  323. }
  324. /**
  325. * vmw_cmdbuf_ctx_submit: Process a command buffer context.
  326. *
  327. * @man: The command buffer manager.
  328. * @ctx: The command buffer context.
  329. *
  330. * Submit command buffers to hardware if possible, and process finished
  331. * buffers. Typically freeing them, but on preemption or error take
  332. * appropriate action. Wake up waiters if appropriate.
  333. */
  334. static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man,
  335. struct vmw_cmdbuf_context *ctx,
  336. int *notempty)
  337. {
  338. struct vmw_cmdbuf_header *entry, *next;
  339. vmw_cmdbuf_ctx_submit(man, ctx);
  340. list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) {
  341. SVGACBStatus status = entry->cb_header->status;
  342. if (status == SVGA_CB_STATUS_NONE)
  343. break;
  344. list_del(&entry->list);
  345. wake_up_all(&man->idle_queue);
  346. ctx->num_hw_submitted--;
  347. switch (status) {
  348. case SVGA_CB_STATUS_COMPLETED:
  349. __vmw_cmdbuf_header_free(entry);
  350. break;
  351. case SVGA_CB_STATUS_COMMAND_ERROR:
  352. case SVGA_CB_STATUS_CB_HEADER_ERROR:
  353. list_add_tail(&entry->list, &man->error);
  354. schedule_work(&man->work);
  355. break;
  356. case SVGA_CB_STATUS_PREEMPTED:
  357. list_add(&entry->list, &ctx->preempted);
  358. break;
  359. default:
  360. WARN_ONCE(true, "Undefined command buffer status.\n");
  361. __vmw_cmdbuf_header_free(entry);
  362. break;
  363. }
  364. }
  365. vmw_cmdbuf_ctx_submit(man, ctx);
  366. if (!list_empty(&ctx->submitted))
  367. (*notempty)++;
  368. }
  369. /**
  370. * vmw_cmdbuf_man_process - Process all command buffer contexts and
  371. * switch on and off irqs as appropriate.
  372. *
  373. * @man: The command buffer manager.
  374. *
  375. * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has
  376. * command buffers left that are not submitted to hardware, Make sure
  377. * IRQ handling is turned on. Otherwise, make sure it's turned off.
  378. */
  379. static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man)
  380. {
  381. int notempty;
  382. struct vmw_cmdbuf_context *ctx;
  383. int i;
  384. retry:
  385. notempty = 0;
  386. for_each_cmdbuf_ctx(man, i, ctx)
  387. vmw_cmdbuf_ctx_process(man, ctx, &notempty);
  388. if (man->irq_on && !notempty) {
  389. vmw_generic_waiter_remove(man->dev_priv,
  390. SVGA_IRQFLAG_COMMAND_BUFFER,
  391. &man->dev_priv->cmdbuf_waiters);
  392. man->irq_on = false;
  393. } else if (!man->irq_on && notempty) {
  394. vmw_generic_waiter_add(man->dev_priv,
  395. SVGA_IRQFLAG_COMMAND_BUFFER,
  396. &man->dev_priv->cmdbuf_waiters);
  397. man->irq_on = true;
  398. /* Rerun in case we just missed an irq. */
  399. goto retry;
  400. }
  401. }
  402. /**
  403. * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a
  404. * command buffer context
  405. *
  406. * @man: The command buffer manager.
  407. * @header: The header of the buffer to submit.
  408. * @cb_context: The command buffer context to use.
  409. *
  410. * This function adds @header to the "submitted" queue of the command
  411. * buffer context identified by @cb_context. It then calls the command buffer
  412. * manager processing to potentially submit the buffer to hardware.
  413. * @man->lock needs to be held when calling this function.
  414. */
  415. static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man,
  416. struct vmw_cmdbuf_header *header,
  417. SVGACBContext cb_context)
  418. {
  419. if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT))
  420. header->cb_header->dxContext = 0;
  421. header->cb_context = cb_context;
  422. list_add_tail(&header->list, &man->ctx[cb_context].submitted);
  423. vmw_cmdbuf_man_process(man);
  424. }
  425. /**
  426. * vmw_cmdbuf_man_tasklet - The main part of the command buffer interrupt
  427. * handler implemented as a tasklet.
  428. *
  429. * @data: Tasklet closure. A pointer to the command buffer manager cast to
  430. * an unsigned long.
  431. *
  432. * The bottom half (tasklet) of the interrupt handler simply calls into the
  433. * command buffer processor to free finished buffers and submit any
  434. * queued buffers to hardware.
  435. */
  436. static void vmw_cmdbuf_man_tasklet(unsigned long data)
  437. {
  438. struct vmw_cmdbuf_man *man = (struct vmw_cmdbuf_man *) data;
  439. spin_lock(&man->lock);
  440. vmw_cmdbuf_man_process(man);
  441. spin_unlock(&man->lock);
  442. }
  443. /**
  444. * vmw_cmdbuf_work_func - The deferred work function that handles
  445. * command buffer errors.
  446. *
  447. * @work: The work func closure argument.
  448. *
  449. * Restarting the command buffer context after an error requires process
  450. * context, so it is deferred to this work function.
  451. */
  452. static void vmw_cmdbuf_work_func(struct work_struct *work)
  453. {
  454. struct vmw_cmdbuf_man *man =
  455. container_of(work, struct vmw_cmdbuf_man, work);
  456. struct vmw_cmdbuf_header *entry, *next;
  457. uint32_t dummy;
  458. bool restart = false;
  459. spin_lock_bh(&man->lock);
  460. list_for_each_entry_safe(entry, next, &man->error, list) {
  461. restart = true;
  462. DRM_ERROR("Command buffer error.\n");
  463. list_del(&entry->list);
  464. __vmw_cmdbuf_header_free(entry);
  465. wake_up_all(&man->idle_queue);
  466. }
  467. spin_unlock_bh(&man->lock);
  468. if (restart && vmw_cmdbuf_startstop(man, true))
  469. DRM_ERROR("Failed restarting command buffer context 0.\n");
  470. /* Send a new fence in case one was removed */
  471. vmw_fifo_send_fence(man->dev_priv, &dummy);
  472. }
  473. /**
  474. * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle.
  475. *
  476. * @man: The command buffer manager.
  477. * @check_preempted: Check also the preempted queue for pending command buffers.
  478. *
  479. */
  480. static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man,
  481. bool check_preempted)
  482. {
  483. struct vmw_cmdbuf_context *ctx;
  484. bool idle = false;
  485. int i;
  486. spin_lock_bh(&man->lock);
  487. vmw_cmdbuf_man_process(man);
  488. for_each_cmdbuf_ctx(man, i, ctx) {
  489. if (!list_empty(&ctx->submitted) ||
  490. !list_empty(&ctx->hw_submitted) ||
  491. (check_preempted && !list_empty(&ctx->preempted)))
  492. goto out_unlock;
  493. }
  494. idle = list_empty(&man->error);
  495. out_unlock:
  496. spin_unlock_bh(&man->lock);
  497. return idle;
  498. }
  499. /**
  500. * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  501. * command submissions
  502. *
  503. * @man: The command buffer manager.
  504. *
  505. * Flushes the current command buffer without allocating a new one. A new one
  506. * is automatically allocated when needed. Call with @man->cur_mutex held.
  507. */
  508. static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man)
  509. {
  510. struct vmw_cmdbuf_header *cur = man->cur;
  511. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  512. if (!cur)
  513. return;
  514. spin_lock_bh(&man->lock);
  515. if (man->cur_pos == 0) {
  516. __vmw_cmdbuf_header_free(cur);
  517. goto out_unlock;
  518. }
  519. man->cur->cb_header->length = man->cur_pos;
  520. vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0);
  521. out_unlock:
  522. spin_unlock_bh(&man->lock);
  523. man->cur = NULL;
  524. man->cur_pos = 0;
  525. }
  526. /**
  527. * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  528. * command submissions
  529. *
  530. * @man: The command buffer manager.
  531. * @interruptible: Whether to sleep interruptible when sleeping.
  532. *
  533. * Flushes the current command buffer without allocating a new one. A new one
  534. * is automatically allocated when needed.
  535. */
  536. int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
  537. bool interruptible)
  538. {
  539. int ret = vmw_cmdbuf_cur_lock(man, interruptible);
  540. if (ret)
  541. return ret;
  542. __vmw_cmdbuf_cur_flush(man);
  543. vmw_cmdbuf_cur_unlock(man);
  544. return 0;
  545. }
  546. /**
  547. * vmw_cmdbuf_idle - Wait for command buffer manager idle.
  548. *
  549. * @man: The command buffer manager.
  550. * @interruptible: Sleep interruptible while waiting.
  551. * @timeout: Time out after this many ticks.
  552. *
  553. * Wait until the command buffer manager has processed all command buffers,
  554. * or until a timeout occurs. If a timeout occurs, the function will return
  555. * -EBUSY.
  556. */
  557. int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
  558. unsigned long timeout)
  559. {
  560. int ret;
  561. ret = vmw_cmdbuf_cur_flush(man, interruptible);
  562. vmw_generic_waiter_add(man->dev_priv,
  563. SVGA_IRQFLAG_COMMAND_BUFFER,
  564. &man->dev_priv->cmdbuf_waiters);
  565. if (interruptible) {
  566. ret = wait_event_interruptible_timeout
  567. (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  568. timeout);
  569. } else {
  570. ret = wait_event_timeout
  571. (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  572. timeout);
  573. }
  574. vmw_generic_waiter_remove(man->dev_priv,
  575. SVGA_IRQFLAG_COMMAND_BUFFER,
  576. &man->dev_priv->cmdbuf_waiters);
  577. if (ret == 0) {
  578. if (!vmw_cmdbuf_man_idle(man, true))
  579. ret = -EBUSY;
  580. else
  581. ret = 0;
  582. }
  583. if (ret > 0)
  584. ret = 0;
  585. return ret;
  586. }
  587. /**
  588. * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool.
  589. *
  590. * @man: The command buffer manager.
  591. * @info: Allocation info. Will hold the size on entry and allocated mm node
  592. * on successful return.
  593. *
  594. * Try to allocate buffer space from the main pool. Returns true if succeeded.
  595. * If a fatal error was hit, the error code is returned in @info->ret.
  596. */
  597. static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man,
  598. struct vmw_cmdbuf_alloc_info *info)
  599. {
  600. int ret;
  601. if (info->done)
  602. return true;
  603. memset(info->node, 0, sizeof(*info->node));
  604. spin_lock_bh(&man->lock);
  605. ret = drm_mm_insert_node_generic(&man->mm, info->node, info->page_size,
  606. 0, 0,
  607. DRM_MM_SEARCH_DEFAULT,
  608. DRM_MM_CREATE_DEFAULT);
  609. if (ret) {
  610. vmw_cmdbuf_man_process(man);
  611. ret = drm_mm_insert_node_generic(&man->mm, info->node,
  612. info->page_size, 0, 0,
  613. DRM_MM_SEARCH_DEFAULT,
  614. DRM_MM_CREATE_DEFAULT);
  615. }
  616. spin_unlock_bh(&man->lock);
  617. info->done = !ret;
  618. return info->done;
  619. }
  620. /**
  621. * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool.
  622. *
  623. * @man: The command buffer manager.
  624. * @node: Pointer to pre-allocated range-manager node.
  625. * @size: The size of the allocation.
  626. * @interruptible: Whether to sleep interruptible while waiting for space.
  627. *
  628. * This function allocates buffer space from the main pool, and if there is
  629. * no space available ATM, it turns on IRQ handling and sleeps waiting for it to
  630. * become available.
  631. */
  632. static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man,
  633. struct drm_mm_node *node,
  634. size_t size,
  635. bool interruptible)
  636. {
  637. struct vmw_cmdbuf_alloc_info info;
  638. info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT;
  639. info.node = node;
  640. info.done = false;
  641. /*
  642. * To prevent starvation of large requests, only one allocating call
  643. * at a time waiting for space.
  644. */
  645. if (interruptible) {
  646. if (mutex_lock_interruptible(&man->space_mutex))
  647. return -ERESTARTSYS;
  648. } else {
  649. mutex_lock(&man->space_mutex);
  650. }
  651. /* Try to allocate space without waiting. */
  652. if (vmw_cmdbuf_try_alloc(man, &info))
  653. goto out_unlock;
  654. vmw_generic_waiter_add(man->dev_priv,
  655. SVGA_IRQFLAG_COMMAND_BUFFER,
  656. &man->dev_priv->cmdbuf_waiters);
  657. if (interruptible) {
  658. int ret;
  659. ret = wait_event_interruptible
  660. (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  661. if (ret) {
  662. vmw_generic_waiter_remove
  663. (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER,
  664. &man->dev_priv->cmdbuf_waiters);
  665. mutex_unlock(&man->space_mutex);
  666. return ret;
  667. }
  668. } else {
  669. wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  670. }
  671. vmw_generic_waiter_remove(man->dev_priv,
  672. SVGA_IRQFLAG_COMMAND_BUFFER,
  673. &man->dev_priv->cmdbuf_waiters);
  674. out_unlock:
  675. mutex_unlock(&man->space_mutex);
  676. return 0;
  677. }
  678. /**
  679. * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer
  680. * space from the main pool.
  681. *
  682. * @man: The command buffer manager.
  683. * @header: Pointer to the header to set up.
  684. * @size: The requested size of the buffer space.
  685. * @interruptible: Whether to sleep interruptible while waiting for space.
  686. */
  687. static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man,
  688. struct vmw_cmdbuf_header *header,
  689. size_t size,
  690. bool interruptible)
  691. {
  692. SVGACBHeader *cb_hdr;
  693. size_t offset;
  694. int ret;
  695. if (!man->has_pool)
  696. return -ENOMEM;
  697. ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible);
  698. if (ret)
  699. return ret;
  700. header->cb_header = dma_pool_alloc(man->headers, GFP_KERNEL,
  701. &header->handle);
  702. if (!header->cb_header) {
  703. ret = -ENOMEM;
  704. goto out_no_cb_header;
  705. }
  706. header->size = header->node.size << PAGE_SHIFT;
  707. cb_hdr = header->cb_header;
  708. offset = header->node.start << PAGE_SHIFT;
  709. header->cmd = man->map + offset;
  710. memset(cb_hdr, 0, sizeof(*cb_hdr));
  711. if (man->using_mob) {
  712. cb_hdr->flags = SVGA_CB_FLAG_MOB;
  713. cb_hdr->ptr.mob.mobid = man->cmd_space->mem.start;
  714. cb_hdr->ptr.mob.mobOffset = offset;
  715. } else {
  716. cb_hdr->ptr.pa = (u64)man->handle + (u64)offset;
  717. }
  718. return 0;
  719. out_no_cb_header:
  720. spin_lock_bh(&man->lock);
  721. drm_mm_remove_node(&header->node);
  722. spin_unlock_bh(&man->lock);
  723. return ret;
  724. }
  725. /**
  726. * vmw_cmdbuf_space_inline - Set up a command buffer header with
  727. * inline command buffer space.
  728. *
  729. * @man: The command buffer manager.
  730. * @header: Pointer to the header to set up.
  731. * @size: The requested size of the buffer space.
  732. */
  733. static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man,
  734. struct vmw_cmdbuf_header *header,
  735. int size)
  736. {
  737. struct vmw_cmdbuf_dheader *dheader;
  738. SVGACBHeader *cb_hdr;
  739. if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE))
  740. return -ENOMEM;
  741. dheader = dma_pool_alloc(man->dheaders, GFP_KERNEL,
  742. &header->handle);
  743. if (!dheader)
  744. return -ENOMEM;
  745. header->inline_space = true;
  746. header->size = VMW_CMDBUF_INLINE_SIZE;
  747. cb_hdr = &dheader->cb_header;
  748. header->cb_header = cb_hdr;
  749. header->cmd = dheader->cmd;
  750. memset(dheader, 0, sizeof(*dheader));
  751. cb_hdr->status = SVGA_CB_STATUS_NONE;
  752. cb_hdr->flags = SVGA_CB_FLAG_NONE;
  753. cb_hdr->ptr.pa = (u64)header->handle +
  754. (u64)offsetof(struct vmw_cmdbuf_dheader, cmd);
  755. return 0;
  756. }
  757. /**
  758. * vmw_cmdbuf_alloc - Allocate a command buffer header complete with
  759. * command buffer space.
  760. *
  761. * @man: The command buffer manager.
  762. * @size: The requested size of the buffer space.
  763. * @interruptible: Whether to sleep interruptible while waiting for space.
  764. * @p_header: points to a header pointer to populate on successful return.
  765. *
  766. * Returns a pointer to command buffer space if successful. Otherwise
  767. * returns an error pointer. The header pointer returned in @p_header should
  768. * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit().
  769. */
  770. void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
  771. size_t size, bool interruptible,
  772. struct vmw_cmdbuf_header **p_header)
  773. {
  774. struct vmw_cmdbuf_header *header;
  775. int ret = 0;
  776. *p_header = NULL;
  777. header = kzalloc(sizeof(*header), GFP_KERNEL);
  778. if (!header)
  779. return ERR_PTR(-ENOMEM);
  780. if (size <= VMW_CMDBUF_INLINE_SIZE)
  781. ret = vmw_cmdbuf_space_inline(man, header, size);
  782. else
  783. ret = vmw_cmdbuf_space_pool(man, header, size, interruptible);
  784. if (ret) {
  785. kfree(header);
  786. return ERR_PTR(ret);
  787. }
  788. header->man = man;
  789. INIT_LIST_HEAD(&header->list);
  790. header->cb_header->status = SVGA_CB_STATUS_NONE;
  791. *p_header = header;
  792. return header->cmd;
  793. }
  794. /**
  795. * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current
  796. * command buffer.
  797. *
  798. * @man: The command buffer manager.
  799. * @size: The requested size of the commands.
  800. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  801. * @interruptible: Whether to sleep interruptible while waiting for space.
  802. *
  803. * Returns a pointer to command buffer space if successful. Otherwise
  804. * returns an error pointer.
  805. */
  806. static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man,
  807. size_t size,
  808. int ctx_id,
  809. bool interruptible)
  810. {
  811. struct vmw_cmdbuf_header *cur;
  812. void *ret;
  813. if (vmw_cmdbuf_cur_lock(man, interruptible))
  814. return ERR_PTR(-ERESTARTSYS);
  815. cur = man->cur;
  816. if (cur && (size + man->cur_pos > cur->size ||
  817. ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) &&
  818. ctx_id != cur->cb_header->dxContext)))
  819. __vmw_cmdbuf_cur_flush(man);
  820. if (!man->cur) {
  821. ret = vmw_cmdbuf_alloc(man,
  822. max_t(size_t, size, man->default_size),
  823. interruptible, &man->cur);
  824. if (IS_ERR(ret)) {
  825. vmw_cmdbuf_cur_unlock(man);
  826. return ret;
  827. }
  828. cur = man->cur;
  829. }
  830. if (ctx_id != SVGA3D_INVALID_ID) {
  831. cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  832. cur->cb_header->dxContext = ctx_id;
  833. }
  834. cur->reserved = size;
  835. return (void *) (man->cur->cmd + man->cur_pos);
  836. }
  837. /**
  838. * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
  839. *
  840. * @man: The command buffer manager.
  841. * @size: The size of the commands actually written.
  842. * @flush: Whether to flush the command buffer immediately.
  843. */
  844. static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man,
  845. size_t size, bool flush)
  846. {
  847. struct vmw_cmdbuf_header *cur = man->cur;
  848. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  849. WARN_ON(size > cur->reserved);
  850. man->cur_pos += size;
  851. if (!size)
  852. cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  853. if (flush)
  854. __vmw_cmdbuf_cur_flush(man);
  855. vmw_cmdbuf_cur_unlock(man);
  856. }
  857. /**
  858. * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
  859. *
  860. * @man: The command buffer manager.
  861. * @size: The requested size of the commands.
  862. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  863. * @interruptible: Whether to sleep interruptible while waiting for space.
  864. * @header: Header of the command buffer. NULL if the current command buffer
  865. * should be used.
  866. *
  867. * Returns a pointer to command buffer space if successful. Otherwise
  868. * returns an error pointer.
  869. */
  870. void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
  871. int ctx_id, bool interruptible,
  872. struct vmw_cmdbuf_header *header)
  873. {
  874. if (!header)
  875. return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible);
  876. if (size > header->size)
  877. return ERR_PTR(-EINVAL);
  878. if (ctx_id != SVGA3D_INVALID_ID) {
  879. header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  880. header->cb_header->dxContext = ctx_id;
  881. }
  882. header->reserved = size;
  883. return header->cmd;
  884. }
  885. /**
  886. * vmw_cmdbuf_commit - Commit commands in a command buffer.
  887. *
  888. * @man: The command buffer manager.
  889. * @size: The size of the commands actually written.
  890. * @header: Header of the command buffer. NULL if the current command buffer
  891. * should be used.
  892. * @flush: Whether to flush the command buffer immediately.
  893. */
  894. void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
  895. struct vmw_cmdbuf_header *header, bool flush)
  896. {
  897. if (!header) {
  898. vmw_cmdbuf_commit_cur(man, size, flush);
  899. return;
  900. }
  901. (void) vmw_cmdbuf_cur_lock(man, false);
  902. __vmw_cmdbuf_cur_flush(man);
  903. WARN_ON(size > header->reserved);
  904. man->cur = header;
  905. man->cur_pos = size;
  906. if (!size)
  907. header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  908. if (flush)
  909. __vmw_cmdbuf_cur_flush(man);
  910. vmw_cmdbuf_cur_unlock(man);
  911. }
  912. /**
  913. * vmw_cmdbuf_tasklet_schedule - Schedule the interrupt handler bottom half.
  914. *
  915. * @man: The command buffer manager.
  916. */
  917. void vmw_cmdbuf_tasklet_schedule(struct vmw_cmdbuf_man *man)
  918. {
  919. if (!man)
  920. return;
  921. tasklet_schedule(&man->tasklet);
  922. }
  923. /**
  924. * vmw_cmdbuf_send_device_command - Send a command through the device context.
  925. *
  926. * @man: The command buffer manager.
  927. * @command: Pointer to the command to send.
  928. * @size: Size of the command.
  929. *
  930. * Synchronously sends a device context command.
  931. */
  932. static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man,
  933. const void *command,
  934. size_t size)
  935. {
  936. struct vmw_cmdbuf_header *header;
  937. int status;
  938. void *cmd = vmw_cmdbuf_alloc(man, size, false, &header);
  939. if (IS_ERR(cmd))
  940. return PTR_ERR(cmd);
  941. memcpy(cmd, command, size);
  942. header->cb_header->length = size;
  943. header->cb_context = SVGA_CB_CONTEXT_DEVICE;
  944. spin_lock_bh(&man->lock);
  945. status = vmw_cmdbuf_header_submit(header);
  946. spin_unlock_bh(&man->lock);
  947. vmw_cmdbuf_header_free(header);
  948. if (status != SVGA_CB_STATUS_COMPLETED) {
  949. DRM_ERROR("Device context command failed with status %d\n",
  950. status);
  951. return -EINVAL;
  952. }
  953. return 0;
  954. }
  955. /**
  956. * vmw_cmdbuf_startstop - Send a start / stop command through the device
  957. * context.
  958. *
  959. * @man: The command buffer manager.
  960. * @enable: Whether to enable or disable the context.
  961. *
  962. * Synchronously sends a device start / stop context command.
  963. */
  964. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man,
  965. bool enable)
  966. {
  967. struct {
  968. uint32 id;
  969. SVGADCCmdStartStop body;
  970. } __packed cmd;
  971. cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT;
  972. cmd.body.enable = (enable) ? 1 : 0;
  973. cmd.body.context = SVGA_CB_CONTEXT_0;
  974. return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
  975. }
  976. /**
  977. * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
  978. *
  979. * @man: The command buffer manager.
  980. * @size: The size of the main space pool.
  981. * @default_size: The default size of the command buffer for small kernel
  982. * submissions.
  983. *
  984. * Set the size and allocate the main command buffer space pool,
  985. * as well as the default size of the command buffer for
  986. * small kernel submissions. If successful, this enables large command
  987. * submissions. Note that this function requires that rudimentary command
  988. * submission is already available and that the MOB memory manager is alive.
  989. * Returns 0 on success. Negative error code on failure.
  990. */
  991. int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man,
  992. size_t size, size_t default_size)
  993. {
  994. struct vmw_private *dev_priv = man->dev_priv;
  995. bool dummy;
  996. int ret;
  997. if (man->has_pool)
  998. return -EINVAL;
  999. /* First, try to allocate a huge chunk of DMA memory */
  1000. size = PAGE_ALIGN(size);
  1001. man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size,
  1002. &man->handle, GFP_KERNEL);
  1003. if (man->map) {
  1004. man->using_mob = false;
  1005. } else {
  1006. /*
  1007. * DMA memory failed. If we can have command buffers in a
  1008. * MOB, try to use that instead. Note that this will
  1009. * actually call into the already enabled manager, when
  1010. * binding the MOB.
  1011. */
  1012. if (!(dev_priv->capabilities & SVGA_CAP_DX))
  1013. return -ENOMEM;
  1014. ret = ttm_bo_create(&dev_priv->bdev, size, ttm_bo_type_device,
  1015. &vmw_mob_ne_placement, 0, false, NULL,
  1016. &man->cmd_space);
  1017. if (ret)
  1018. return ret;
  1019. man->using_mob = true;
  1020. ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT,
  1021. &man->map_obj);
  1022. if (ret)
  1023. goto out_no_map;
  1024. man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy);
  1025. }
  1026. man->size = size;
  1027. drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT);
  1028. man->has_pool = true;
  1029. /*
  1030. * For now, set the default size to VMW_CMDBUF_INLINE_SIZE to
  1031. * prevent deadlocks from happening when vmw_cmdbuf_space_pool()
  1032. * needs to wait for space and we block on further command
  1033. * submissions to be able to free up space.
  1034. */
  1035. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1036. DRM_INFO("Using command buffers with %s pool.\n",
  1037. (man->using_mob) ? "MOB" : "DMA");
  1038. return 0;
  1039. out_no_map:
  1040. if (man->using_mob)
  1041. ttm_bo_unref(&man->cmd_space);
  1042. return ret;
  1043. }
  1044. /**
  1045. * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
  1046. * inline command buffer submissions only.
  1047. *
  1048. * @dev_priv: Pointer to device private structure.
  1049. *
  1050. * Returns a pointer to a cummand buffer manager to success or error pointer
  1051. * on failure. The command buffer manager will be enabled for submissions of
  1052. * size VMW_CMDBUF_INLINE_SIZE only.
  1053. */
  1054. struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv)
  1055. {
  1056. struct vmw_cmdbuf_man *man;
  1057. struct vmw_cmdbuf_context *ctx;
  1058. int i;
  1059. int ret;
  1060. if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS))
  1061. return ERR_PTR(-ENOSYS);
  1062. man = kzalloc(sizeof(*man), GFP_KERNEL);
  1063. if (!man)
  1064. return ERR_PTR(-ENOMEM);
  1065. man->headers = dma_pool_create("vmwgfx cmdbuf",
  1066. &dev_priv->dev->pdev->dev,
  1067. sizeof(SVGACBHeader),
  1068. 64, PAGE_SIZE);
  1069. if (!man->headers) {
  1070. ret = -ENOMEM;
  1071. goto out_no_pool;
  1072. }
  1073. man->dheaders = dma_pool_create("vmwgfx inline cmdbuf",
  1074. &dev_priv->dev->pdev->dev,
  1075. sizeof(struct vmw_cmdbuf_dheader),
  1076. 64, PAGE_SIZE);
  1077. if (!man->dheaders) {
  1078. ret = -ENOMEM;
  1079. goto out_no_dpool;
  1080. }
  1081. for_each_cmdbuf_ctx(man, i, ctx)
  1082. vmw_cmdbuf_ctx_init(ctx);
  1083. INIT_LIST_HEAD(&man->error);
  1084. spin_lock_init(&man->lock);
  1085. mutex_init(&man->cur_mutex);
  1086. mutex_init(&man->space_mutex);
  1087. tasklet_init(&man->tasklet, vmw_cmdbuf_man_tasklet,
  1088. (unsigned long) man);
  1089. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1090. init_waitqueue_head(&man->alloc_queue);
  1091. init_waitqueue_head(&man->idle_queue);
  1092. man->dev_priv = dev_priv;
  1093. man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1;
  1094. INIT_WORK(&man->work, &vmw_cmdbuf_work_func);
  1095. vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR,
  1096. &dev_priv->error_waiters);
  1097. ret = vmw_cmdbuf_startstop(man, true);
  1098. if (ret) {
  1099. DRM_ERROR("Failed starting command buffer context 0.\n");
  1100. vmw_cmdbuf_man_destroy(man);
  1101. return ERR_PTR(ret);
  1102. }
  1103. return man;
  1104. out_no_dpool:
  1105. dma_pool_destroy(man->headers);
  1106. out_no_pool:
  1107. kfree(man);
  1108. return ERR_PTR(ret);
  1109. }
  1110. /**
  1111. * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
  1112. *
  1113. * @man: Pointer to a command buffer manager.
  1114. *
  1115. * This function removes the main buffer space pool, and should be called
  1116. * before MOB memory management is removed. When this function has been called,
  1117. * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
  1118. * less are allowed, and the default size of the command buffer for small kernel
  1119. * submissions is also set to this size.
  1120. */
  1121. void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man)
  1122. {
  1123. if (!man->has_pool)
  1124. return;
  1125. man->has_pool = false;
  1126. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1127. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1128. if (man->using_mob) {
  1129. (void) ttm_bo_kunmap(&man->map_obj);
  1130. ttm_bo_unref(&man->cmd_space);
  1131. } else {
  1132. dma_free_coherent(&man->dev_priv->dev->pdev->dev,
  1133. man->size, man->map, man->handle);
  1134. }
  1135. }
  1136. /**
  1137. * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
  1138. *
  1139. * @man: Pointer to a command buffer manager.
  1140. *
  1141. * This function idles and then destroys a command buffer manager.
  1142. */
  1143. void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man)
  1144. {
  1145. WARN_ON_ONCE(man->has_pool);
  1146. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1147. if (vmw_cmdbuf_startstop(man, false))
  1148. DRM_ERROR("Failed stopping command buffer context 0.\n");
  1149. vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR,
  1150. &man->dev_priv->error_waiters);
  1151. tasklet_kill(&man->tasklet);
  1152. (void) cancel_work_sync(&man->work);
  1153. dma_pool_destroy(man->dheaders);
  1154. dma_pool_destroy(man->headers);
  1155. mutex_destroy(&man->cur_mutex);
  1156. mutex_destroy(&man->space_mutex);
  1157. kfree(man);
  1158. }