vmwgfx_cmdbuf.c 39 KB

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