vmwgfx_shader.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2009-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 <drm/ttm/ttm_placement.h>
  28. #include "vmwgfx_binding.h"
  29. #include "vmwgfx_bo.h"
  30. #include "vmwgfx_drv.h"
  31. #include "vmwgfx_resource_priv.h"
  32. struct vmw_shader {
  33. struct vmw_resource res;
  34. SVGA3dShaderType type;
  35. uint32_t size;
  36. uint8_t num_input_sig;
  37. uint8_t num_output_sig;
  38. };
  39. struct vmw_user_shader {
  40. struct ttm_base_object base;
  41. struct vmw_shader shader;
  42. };
  43. struct vmw_dx_shader {
  44. struct vmw_resource res;
  45. struct vmw_resource *ctx;
  46. struct vmw_resource *cotable;
  47. u32 id;
  48. bool committed;
  49. struct list_head cotable_head;
  50. };
  51. static void vmw_user_shader_free(struct vmw_resource *res);
  52. static struct vmw_resource *
  53. vmw_user_shader_base_to_res(struct ttm_base_object *base);
  54. static int vmw_gb_shader_create(struct vmw_resource *res);
  55. static int vmw_gb_shader_bind(struct vmw_resource *res,
  56. struct ttm_validate_buffer *val_buf);
  57. static int vmw_gb_shader_unbind(struct vmw_resource *res,
  58. bool readback,
  59. struct ttm_validate_buffer *val_buf);
  60. static int vmw_gb_shader_destroy(struct vmw_resource *res);
  61. static int vmw_dx_shader_create(struct vmw_resource *res);
  62. static int vmw_dx_shader_bind(struct vmw_resource *res,
  63. struct ttm_validate_buffer *val_buf);
  64. static int vmw_dx_shader_unbind(struct vmw_resource *res,
  65. bool readback,
  66. struct ttm_validate_buffer *val_buf);
  67. static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
  68. enum vmw_cmdbuf_res_state state);
  69. static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type);
  70. static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type);
  71. static const struct vmw_user_resource_conv user_shader_conv = {
  72. .object_type = VMW_RES_SHADER,
  73. .base_obj_to_res = vmw_user_shader_base_to_res,
  74. .res_free = vmw_user_shader_free
  75. };
  76. const struct vmw_user_resource_conv *user_shader_converter =
  77. &user_shader_conv;
  78. static const struct vmw_res_func vmw_gb_shader_func = {
  79. .res_type = vmw_res_shader,
  80. .needs_guest_memory = true,
  81. .may_evict = true,
  82. .prio = 3,
  83. .dirty_prio = 3,
  84. .type_name = "guest backed shaders",
  85. .domain = VMW_BO_DOMAIN_MOB,
  86. .busy_domain = VMW_BO_DOMAIN_MOB,
  87. .create = vmw_gb_shader_create,
  88. .destroy = vmw_gb_shader_destroy,
  89. .bind = vmw_gb_shader_bind,
  90. .unbind = vmw_gb_shader_unbind
  91. };
  92. static const struct vmw_res_func vmw_dx_shader_func = {
  93. .res_type = vmw_res_shader,
  94. .needs_guest_memory = true,
  95. .may_evict = true,
  96. .prio = 3,
  97. .dirty_prio = 3,
  98. .type_name = "dx shaders",
  99. .domain = VMW_BO_DOMAIN_MOB,
  100. .busy_domain = VMW_BO_DOMAIN_MOB,
  101. .create = vmw_dx_shader_create,
  102. /*
  103. * The destroy callback is only called with a committed resource on
  104. * context destroy, in which case we destroy the cotable anyway,
  105. * so there's no need to destroy DX shaders separately.
  106. */
  107. .destroy = NULL,
  108. .bind = vmw_dx_shader_bind,
  109. .unbind = vmw_dx_shader_unbind,
  110. .commit_notify = vmw_dx_shader_commit_notify,
  111. };
  112. /*
  113. * Shader management:
  114. */
  115. static inline struct vmw_shader *
  116. vmw_res_to_shader(struct vmw_resource *res)
  117. {
  118. return container_of(res, struct vmw_shader, res);
  119. }
  120. /**
  121. * vmw_res_to_dx_shader - typecast a struct vmw_resource to a
  122. * struct vmw_dx_shader
  123. *
  124. * @res: Pointer to the struct vmw_resource.
  125. */
  126. static inline struct vmw_dx_shader *
  127. vmw_res_to_dx_shader(struct vmw_resource *res)
  128. {
  129. return container_of(res, struct vmw_dx_shader, res);
  130. }
  131. static void vmw_hw_shader_destroy(struct vmw_resource *res)
  132. {
  133. if (likely(res->func->destroy))
  134. (void) res->func->destroy(res);
  135. else
  136. res->id = -1;
  137. }
  138. static int vmw_gb_shader_init(struct vmw_private *dev_priv,
  139. struct vmw_resource *res,
  140. uint32_t size,
  141. uint64_t offset,
  142. SVGA3dShaderType type,
  143. uint8_t num_input_sig,
  144. uint8_t num_output_sig,
  145. struct vmw_bo *byte_code,
  146. void (*res_free) (struct vmw_resource *res))
  147. {
  148. struct vmw_shader *shader = vmw_res_to_shader(res);
  149. int ret;
  150. ret = vmw_resource_init(dev_priv, res, true, res_free,
  151. &vmw_gb_shader_func);
  152. if (unlikely(ret != 0)) {
  153. if (res_free)
  154. res_free(res);
  155. else
  156. kfree(res);
  157. return ret;
  158. }
  159. res->guest_memory_size = size;
  160. if (byte_code) {
  161. res->guest_memory_bo = vmw_user_bo_ref(byte_code);
  162. res->guest_memory_offset = offset;
  163. }
  164. shader->size = size;
  165. shader->type = type;
  166. shader->num_input_sig = num_input_sig;
  167. shader->num_output_sig = num_output_sig;
  168. res->hw_destroy = vmw_hw_shader_destroy;
  169. return 0;
  170. }
  171. /*
  172. * GB shader code:
  173. */
  174. static int vmw_gb_shader_create(struct vmw_resource *res)
  175. {
  176. struct vmw_private *dev_priv = res->dev_priv;
  177. struct vmw_shader *shader = vmw_res_to_shader(res);
  178. int ret;
  179. struct {
  180. SVGA3dCmdHeader header;
  181. SVGA3dCmdDefineGBShader body;
  182. } *cmd;
  183. if (likely(res->id != -1))
  184. return 0;
  185. ret = vmw_resource_alloc_id(res);
  186. if (unlikely(ret != 0)) {
  187. DRM_ERROR("Failed to allocate a shader id.\n");
  188. goto out_no_id;
  189. }
  190. if (unlikely(res->id >= VMWGFX_NUM_GB_SHADER)) {
  191. ret = -EBUSY;
  192. goto out_no_fifo;
  193. }
  194. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  195. if (unlikely(cmd == NULL)) {
  196. ret = -ENOMEM;
  197. goto out_no_fifo;
  198. }
  199. cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SHADER;
  200. cmd->header.size = sizeof(cmd->body);
  201. cmd->body.shid = res->id;
  202. cmd->body.type = shader->type;
  203. cmd->body.sizeInBytes = shader->size;
  204. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  205. vmw_fifo_resource_inc(dev_priv);
  206. return 0;
  207. out_no_fifo:
  208. vmw_resource_release_id(res);
  209. out_no_id:
  210. return ret;
  211. }
  212. static int vmw_gb_shader_bind(struct vmw_resource *res,
  213. struct ttm_validate_buffer *val_buf)
  214. {
  215. struct vmw_private *dev_priv = res->dev_priv;
  216. struct {
  217. SVGA3dCmdHeader header;
  218. SVGA3dCmdBindGBShader body;
  219. } *cmd;
  220. struct ttm_buffer_object *bo = val_buf->bo;
  221. BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
  222. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  223. if (unlikely(cmd == NULL))
  224. return -ENOMEM;
  225. cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
  226. cmd->header.size = sizeof(cmd->body);
  227. cmd->body.shid = res->id;
  228. cmd->body.mobid = bo->resource->start;
  229. cmd->body.offsetInBytes = res->guest_memory_offset;
  230. res->guest_memory_dirty = false;
  231. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  232. return 0;
  233. }
  234. static int vmw_gb_shader_unbind(struct vmw_resource *res,
  235. bool readback,
  236. struct ttm_validate_buffer *val_buf)
  237. {
  238. struct vmw_private *dev_priv = res->dev_priv;
  239. struct {
  240. SVGA3dCmdHeader header;
  241. SVGA3dCmdBindGBShader body;
  242. } *cmd;
  243. struct vmw_fence_obj *fence;
  244. BUG_ON(res->guest_memory_bo->tbo.resource->mem_type != VMW_PL_MOB);
  245. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  246. if (unlikely(cmd == NULL))
  247. return -ENOMEM;
  248. cmd->header.id = SVGA_3D_CMD_BIND_GB_SHADER;
  249. cmd->header.size = sizeof(cmd->body);
  250. cmd->body.shid = res->id;
  251. cmd->body.mobid = SVGA3D_INVALID_ID;
  252. cmd->body.offsetInBytes = 0;
  253. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  254. /*
  255. * Create a fence object and fence the backup buffer.
  256. */
  257. (void) vmw_execbuf_fence_commands(NULL, dev_priv,
  258. &fence, NULL);
  259. vmw_bo_fence_single(val_buf->bo, fence);
  260. if (likely(fence != NULL))
  261. vmw_fence_obj_unreference(&fence);
  262. return 0;
  263. }
  264. static int vmw_gb_shader_destroy(struct vmw_resource *res)
  265. {
  266. struct vmw_private *dev_priv = res->dev_priv;
  267. struct {
  268. SVGA3dCmdHeader header;
  269. SVGA3dCmdDestroyGBShader body;
  270. } *cmd;
  271. if (likely(res->id == -1))
  272. return 0;
  273. mutex_lock(&dev_priv->binding_mutex);
  274. vmw_binding_res_list_scrub(&res->binding_head);
  275. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  276. if (unlikely(cmd == NULL)) {
  277. mutex_unlock(&dev_priv->binding_mutex);
  278. return -ENOMEM;
  279. }
  280. cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SHADER;
  281. cmd->header.size = sizeof(cmd->body);
  282. cmd->body.shid = res->id;
  283. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  284. mutex_unlock(&dev_priv->binding_mutex);
  285. vmw_resource_release_id(res);
  286. vmw_fifo_resource_dec(dev_priv);
  287. return 0;
  288. }
  289. /*
  290. * DX shader code:
  291. */
  292. /**
  293. * vmw_dx_shader_commit_notify - Notify that a shader operation has been
  294. * committed to hardware from a user-supplied command stream.
  295. *
  296. * @res: Pointer to the shader resource.
  297. * @state: Indicating whether a creation or removal has been committed.
  298. *
  299. */
  300. static void vmw_dx_shader_commit_notify(struct vmw_resource *res,
  301. enum vmw_cmdbuf_res_state state)
  302. {
  303. struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
  304. struct vmw_private *dev_priv = res->dev_priv;
  305. if (state == VMW_CMDBUF_RES_ADD) {
  306. mutex_lock(&dev_priv->binding_mutex);
  307. vmw_cotable_add_resource(shader->cotable,
  308. &shader->cotable_head);
  309. shader->committed = true;
  310. res->id = shader->id;
  311. mutex_unlock(&dev_priv->binding_mutex);
  312. } else {
  313. mutex_lock(&dev_priv->binding_mutex);
  314. list_del_init(&shader->cotable_head);
  315. shader->committed = false;
  316. res->id = -1;
  317. mutex_unlock(&dev_priv->binding_mutex);
  318. }
  319. }
  320. /**
  321. * vmw_dx_shader_unscrub - Have the device reattach a MOB to a DX shader.
  322. *
  323. * @res: The shader resource
  324. *
  325. * This function reverts a scrub operation.
  326. */
  327. static int vmw_dx_shader_unscrub(struct vmw_resource *res)
  328. {
  329. struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
  330. struct vmw_private *dev_priv = res->dev_priv;
  331. struct {
  332. SVGA3dCmdHeader header;
  333. SVGA3dCmdDXBindShader body;
  334. } *cmd;
  335. if (!list_empty(&shader->cotable_head) || !shader->committed)
  336. return 0;
  337. cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), shader->ctx->id);
  338. if (unlikely(cmd == NULL))
  339. return -ENOMEM;
  340. cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
  341. cmd->header.size = sizeof(cmd->body);
  342. cmd->body.cid = shader->ctx->id;
  343. cmd->body.shid = shader->id;
  344. cmd->body.mobid = res->guest_memory_bo->tbo.resource->start;
  345. cmd->body.offsetInBytes = res->guest_memory_offset;
  346. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  347. vmw_cotable_add_resource(shader->cotable, &shader->cotable_head);
  348. return 0;
  349. }
  350. /**
  351. * vmw_dx_shader_create - The DX shader create callback
  352. *
  353. * @res: The DX shader resource
  354. *
  355. * The create callback is called as part of resource validation and
  356. * makes sure that we unscrub the shader if it's previously been scrubbed.
  357. */
  358. static int vmw_dx_shader_create(struct vmw_resource *res)
  359. {
  360. struct vmw_private *dev_priv = res->dev_priv;
  361. struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
  362. int ret = 0;
  363. WARN_ON_ONCE(!shader->committed);
  364. if (vmw_resource_mob_attached(res)) {
  365. mutex_lock(&dev_priv->binding_mutex);
  366. ret = vmw_dx_shader_unscrub(res);
  367. mutex_unlock(&dev_priv->binding_mutex);
  368. }
  369. res->id = shader->id;
  370. return ret;
  371. }
  372. /**
  373. * vmw_dx_shader_bind - The DX shader bind callback
  374. *
  375. * @res: The DX shader resource
  376. * @val_buf: Pointer to the validate buffer.
  377. *
  378. */
  379. static int vmw_dx_shader_bind(struct vmw_resource *res,
  380. struct ttm_validate_buffer *val_buf)
  381. {
  382. struct vmw_private *dev_priv = res->dev_priv;
  383. struct ttm_buffer_object *bo = val_buf->bo;
  384. BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
  385. mutex_lock(&dev_priv->binding_mutex);
  386. vmw_dx_shader_unscrub(res);
  387. mutex_unlock(&dev_priv->binding_mutex);
  388. return 0;
  389. }
  390. /**
  391. * vmw_dx_shader_scrub - Have the device unbind a MOB from a DX shader.
  392. *
  393. * @res: The shader resource
  394. *
  395. * This function unbinds a MOB from the DX shader without requiring the
  396. * MOB dma_buffer to be reserved. The driver still considers the MOB bound.
  397. * However, once the driver eventually decides to unbind the MOB, it doesn't
  398. * need to access the context.
  399. */
  400. static int vmw_dx_shader_scrub(struct vmw_resource *res)
  401. {
  402. struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
  403. struct vmw_private *dev_priv = res->dev_priv;
  404. struct {
  405. SVGA3dCmdHeader header;
  406. SVGA3dCmdDXBindShader body;
  407. } *cmd;
  408. if (list_empty(&shader->cotable_head))
  409. return 0;
  410. WARN_ON_ONCE(!shader->committed);
  411. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  412. if (unlikely(cmd == NULL))
  413. return -ENOMEM;
  414. cmd->header.id = SVGA_3D_CMD_DX_BIND_SHADER;
  415. cmd->header.size = sizeof(cmd->body);
  416. cmd->body.cid = shader->ctx->id;
  417. cmd->body.shid = res->id;
  418. cmd->body.mobid = SVGA3D_INVALID_ID;
  419. cmd->body.offsetInBytes = 0;
  420. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  421. res->id = -1;
  422. list_del_init(&shader->cotable_head);
  423. return 0;
  424. }
  425. /**
  426. * vmw_dx_shader_unbind - The dx shader unbind callback.
  427. *
  428. * @res: The shader resource
  429. * @readback: Whether this is a readback unbind. Currently unused.
  430. * @val_buf: MOB buffer information.
  431. */
  432. static int vmw_dx_shader_unbind(struct vmw_resource *res,
  433. bool readback,
  434. struct ttm_validate_buffer *val_buf)
  435. {
  436. struct vmw_private *dev_priv = res->dev_priv;
  437. struct vmw_fence_obj *fence;
  438. int ret;
  439. BUG_ON(res->guest_memory_bo->tbo.resource->mem_type != VMW_PL_MOB);
  440. mutex_lock(&dev_priv->binding_mutex);
  441. ret = vmw_dx_shader_scrub(res);
  442. mutex_unlock(&dev_priv->binding_mutex);
  443. if (ret)
  444. return ret;
  445. (void) vmw_execbuf_fence_commands(NULL, dev_priv,
  446. &fence, NULL);
  447. vmw_bo_fence_single(val_buf->bo, fence);
  448. if (likely(fence != NULL))
  449. vmw_fence_obj_unreference(&fence);
  450. return 0;
  451. }
  452. /**
  453. * vmw_dx_shader_cotable_list_scrub - The cotable unbind_func callback for
  454. * DX shaders.
  455. *
  456. * @dev_priv: Pointer to device private structure.
  457. * @list: The list of cotable resources.
  458. * @readback: Whether the call was part of a readback unbind.
  459. *
  460. * Scrubs all shader MOBs so that any subsequent shader unbind or shader
  461. * destroy operation won't need to swap in the context.
  462. */
  463. void vmw_dx_shader_cotable_list_scrub(struct vmw_private *dev_priv,
  464. struct list_head *list,
  465. bool readback)
  466. {
  467. struct vmw_dx_shader *entry, *next;
  468. lockdep_assert_held_once(&dev_priv->binding_mutex);
  469. list_for_each_entry_safe(entry, next, list, cotable_head) {
  470. WARN_ON(vmw_dx_shader_scrub(&entry->res));
  471. if (!readback)
  472. entry->committed = false;
  473. }
  474. }
  475. /**
  476. * vmw_dx_shader_res_free - The DX shader free callback
  477. *
  478. * @res: The shader resource
  479. *
  480. * Frees the DX shader resource.
  481. */
  482. static void vmw_dx_shader_res_free(struct vmw_resource *res)
  483. {
  484. struct vmw_dx_shader *shader = vmw_res_to_dx_shader(res);
  485. vmw_resource_unreference(&shader->cotable);
  486. kfree(shader);
  487. }
  488. /**
  489. * vmw_dx_shader_add - Add a shader resource as a command buffer managed
  490. * resource.
  491. *
  492. * @man: The command buffer resource manager.
  493. * @ctx: Pointer to the context resource.
  494. * @user_key: The id used for this shader.
  495. * @shader_type: The shader type.
  496. * @list: The list of staged command buffer managed resources.
  497. */
  498. int vmw_dx_shader_add(struct vmw_cmdbuf_res_manager *man,
  499. struct vmw_resource *ctx,
  500. u32 user_key,
  501. SVGA3dShaderType shader_type,
  502. struct list_head *list)
  503. {
  504. struct vmw_dx_shader *shader;
  505. struct vmw_resource *res;
  506. struct vmw_private *dev_priv = ctx->dev_priv;
  507. int ret;
  508. if (!vmw_shader_id_ok(user_key, shader_type))
  509. return -EINVAL;
  510. shader = kmalloc_obj(*shader);
  511. if (!shader) {
  512. return -ENOMEM;
  513. }
  514. res = &shader->res;
  515. shader->ctx = ctx;
  516. shader->cotable = vmw_resource_reference
  517. (vmw_context_cotable(ctx, SVGA_COTABLE_DXSHADER));
  518. shader->id = user_key;
  519. shader->committed = false;
  520. INIT_LIST_HEAD(&shader->cotable_head);
  521. ret = vmw_resource_init(dev_priv, res, true,
  522. vmw_dx_shader_res_free, &vmw_dx_shader_func);
  523. if (ret)
  524. goto out_resource_init;
  525. /*
  526. * The user_key name-space is not per shader type for DX shaders,
  527. * so when hashing, use a single zero shader type.
  528. */
  529. ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
  530. vmw_shader_key(user_key, 0),
  531. res, list);
  532. if (ret)
  533. goto out_resource_init;
  534. res->id = shader->id;
  535. res->hw_destroy = vmw_hw_shader_destroy;
  536. out_resource_init:
  537. vmw_resource_unreference(&res);
  538. return ret;
  539. }
  540. /*
  541. * User-space shader management:
  542. */
  543. static struct vmw_resource *
  544. vmw_user_shader_base_to_res(struct ttm_base_object *base)
  545. {
  546. return &(container_of(base, struct vmw_user_shader, base)->
  547. shader.res);
  548. }
  549. static void vmw_user_shader_free(struct vmw_resource *res)
  550. {
  551. struct vmw_user_shader *ushader =
  552. container_of(res, struct vmw_user_shader, shader.res);
  553. ttm_base_object_kfree(ushader, base);
  554. }
  555. static void vmw_shader_free(struct vmw_resource *res)
  556. {
  557. struct vmw_shader *shader = vmw_res_to_shader(res);
  558. kfree(shader);
  559. }
  560. /*
  561. * This function is called when user space has no more references on the
  562. * base object. It releases the base-object's reference on the resource object.
  563. */
  564. static void vmw_user_shader_base_release(struct ttm_base_object **p_base)
  565. {
  566. struct ttm_base_object *base = *p_base;
  567. struct vmw_resource *res = vmw_user_shader_base_to_res(base);
  568. *p_base = NULL;
  569. vmw_resource_unreference(&res);
  570. }
  571. int vmw_shader_destroy_ioctl(struct drm_device *dev, void *data,
  572. struct drm_file *file_priv)
  573. {
  574. struct drm_vmw_shader_arg *arg = (struct drm_vmw_shader_arg *)data;
  575. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  576. return ttm_ref_object_base_unref(tfile, arg->handle);
  577. }
  578. static int vmw_user_shader_alloc(struct vmw_private *dev_priv,
  579. struct vmw_bo *buffer,
  580. size_t shader_size,
  581. size_t offset,
  582. SVGA3dShaderType shader_type,
  583. uint8_t num_input_sig,
  584. uint8_t num_output_sig,
  585. struct ttm_object_file *tfile,
  586. u32 *handle)
  587. {
  588. struct vmw_user_shader *ushader;
  589. struct vmw_resource *res, *tmp;
  590. int ret;
  591. ushader = kzalloc_obj(*ushader);
  592. if (unlikely(!ushader)) {
  593. ret = -ENOMEM;
  594. goto out;
  595. }
  596. res = &ushader->shader.res;
  597. ushader->base.shareable = false;
  598. ushader->base.tfile = NULL;
  599. /*
  600. * From here on, the destructor takes over resource freeing.
  601. */
  602. ret = vmw_gb_shader_init(dev_priv, res, shader_size,
  603. offset, shader_type, num_input_sig,
  604. num_output_sig, buffer,
  605. vmw_user_shader_free);
  606. if (unlikely(ret != 0))
  607. goto out;
  608. tmp = vmw_resource_reference(res);
  609. ret = ttm_base_object_init(tfile, &ushader->base, false,
  610. VMW_RES_SHADER,
  611. &vmw_user_shader_base_release);
  612. if (unlikely(ret != 0)) {
  613. vmw_resource_unreference(&tmp);
  614. goto out_err;
  615. }
  616. if (handle)
  617. *handle = ushader->base.handle;
  618. out_err:
  619. vmw_resource_unreference(&res);
  620. out:
  621. return ret;
  622. }
  623. static struct vmw_resource *vmw_shader_alloc(struct vmw_private *dev_priv,
  624. struct vmw_bo *buffer,
  625. size_t shader_size,
  626. size_t offset,
  627. SVGA3dShaderType shader_type)
  628. {
  629. struct vmw_shader *shader;
  630. struct vmw_resource *res;
  631. int ret;
  632. shader = kzalloc_obj(*shader);
  633. if (unlikely(!shader)) {
  634. ret = -ENOMEM;
  635. goto out_err;
  636. }
  637. res = &shader->res;
  638. /*
  639. * From here on, the destructor takes over resource freeing.
  640. */
  641. ret = vmw_gb_shader_init(dev_priv, res, shader_size,
  642. offset, shader_type, 0, 0, buffer,
  643. vmw_shader_free);
  644. out_err:
  645. return ret ? ERR_PTR(ret) : res;
  646. }
  647. static int vmw_shader_define(struct drm_device *dev, struct drm_file *file_priv,
  648. enum drm_vmw_shader_type shader_type_drm,
  649. u32 buffer_handle, size_t size, size_t offset,
  650. uint8_t num_input_sig, uint8_t num_output_sig,
  651. uint32_t *shader_handle)
  652. {
  653. struct vmw_private *dev_priv = vmw_priv(dev);
  654. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  655. struct vmw_bo *buffer = NULL;
  656. SVGA3dShaderType shader_type;
  657. int ret;
  658. if (buffer_handle != SVGA3D_INVALID_ID) {
  659. ret = vmw_user_bo_lookup(file_priv, buffer_handle, &buffer);
  660. if (unlikely(ret != 0)) {
  661. VMW_DEBUG_USER("Couldn't find buffer for shader creation.\n");
  662. return ret;
  663. }
  664. if ((u64)buffer->tbo.base.size < (u64)size + (u64)offset) {
  665. VMW_DEBUG_USER("Illegal buffer- or shader size.\n");
  666. ret = -EINVAL;
  667. goto out_bad_arg;
  668. }
  669. }
  670. switch (shader_type_drm) {
  671. case drm_vmw_shader_type_vs:
  672. shader_type = SVGA3D_SHADERTYPE_VS;
  673. break;
  674. case drm_vmw_shader_type_ps:
  675. shader_type = SVGA3D_SHADERTYPE_PS;
  676. break;
  677. default:
  678. VMW_DEBUG_USER("Illegal shader type.\n");
  679. ret = -EINVAL;
  680. goto out_bad_arg;
  681. }
  682. ret = vmw_user_shader_alloc(dev_priv, buffer, size, offset,
  683. shader_type, num_input_sig,
  684. num_output_sig, tfile, shader_handle);
  685. out_bad_arg:
  686. vmw_user_bo_unref(&buffer);
  687. return ret;
  688. }
  689. /**
  690. * vmw_shader_id_ok - Check whether a compat shader user key and
  691. * shader type are within valid bounds.
  692. *
  693. * @user_key: User space id of the shader.
  694. * @shader_type: Shader type.
  695. *
  696. * Returns true if valid false if not.
  697. */
  698. static bool vmw_shader_id_ok(u32 user_key, SVGA3dShaderType shader_type)
  699. {
  700. return user_key <= ((1 << 20) - 1) && (unsigned) shader_type < 16;
  701. }
  702. /**
  703. * vmw_shader_key - Compute a hash key suitable for a compat shader.
  704. *
  705. * @user_key: User space id of the shader.
  706. * @shader_type: Shader type.
  707. *
  708. * Returns a hash key suitable for a command buffer managed resource
  709. * manager hash table.
  710. */
  711. static u32 vmw_shader_key(u32 user_key, SVGA3dShaderType shader_type)
  712. {
  713. return user_key | (shader_type << 20);
  714. }
  715. /**
  716. * vmw_shader_remove - Stage a compat shader for removal.
  717. *
  718. * @man: Pointer to the compat shader manager identifying the shader namespace.
  719. * @user_key: The key that is used to identify the shader. The key is
  720. * unique to the shader type.
  721. * @shader_type: Shader type.
  722. * @list: Caller's list of staged command buffer resource actions.
  723. */
  724. int vmw_shader_remove(struct vmw_cmdbuf_res_manager *man,
  725. u32 user_key, SVGA3dShaderType shader_type,
  726. struct list_head *list)
  727. {
  728. struct vmw_resource *dummy;
  729. if (!vmw_shader_id_ok(user_key, shader_type))
  730. return -EINVAL;
  731. return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_shader,
  732. vmw_shader_key(user_key, shader_type),
  733. list, &dummy);
  734. }
  735. /**
  736. * vmw_compat_shader_add - Create a compat shader and stage it for addition
  737. * as a command buffer managed resource.
  738. *
  739. * @dev_priv: Pointer to device private structure.
  740. * @man: Pointer to the compat shader manager identifying the shader namespace.
  741. * @user_key: The key that is used to identify the shader. The key is
  742. * unique to the shader type.
  743. * @bytecode: Pointer to the bytecode of the shader.
  744. * @shader_type: Shader type.
  745. * @size: Command size.
  746. * @list: Caller's list of staged command buffer resource actions.
  747. *
  748. */
  749. int vmw_compat_shader_add(struct vmw_private *dev_priv,
  750. struct vmw_cmdbuf_res_manager *man,
  751. u32 user_key, const void *bytecode,
  752. SVGA3dShaderType shader_type,
  753. size_t size,
  754. struct list_head *list)
  755. {
  756. struct ttm_operation_ctx ctx = { false, true };
  757. struct vmw_bo *buf;
  758. struct ttm_bo_kmap_obj map;
  759. bool is_iomem;
  760. int ret;
  761. struct vmw_resource *res;
  762. struct vmw_bo_params bo_params = {
  763. .domain = VMW_BO_DOMAIN_SYS,
  764. .busy_domain = VMW_BO_DOMAIN_SYS,
  765. .bo_type = ttm_bo_type_device,
  766. .size = size,
  767. .pin = false,
  768. .keep_resv = true,
  769. };
  770. if (!vmw_shader_id_ok(user_key, shader_type))
  771. return -EINVAL;
  772. ret = vmw_bo_create(dev_priv, &bo_params, &buf);
  773. if (unlikely(ret != 0))
  774. goto out;
  775. /* Map and copy shader bytecode. */
  776. ret = ttm_bo_kmap(&buf->tbo, 0, PFN_UP(size), &map);
  777. if (unlikely(ret != 0)) {
  778. ttm_bo_unreserve(&buf->tbo);
  779. goto no_reserve;
  780. }
  781. memcpy(ttm_kmap_obj_virtual(&map, &is_iomem), bytecode, size);
  782. WARN_ON(is_iomem);
  783. ttm_bo_kunmap(&map);
  784. ret = ttm_bo_validate(&buf->tbo, &buf->placement, &ctx);
  785. WARN_ON(ret != 0);
  786. ttm_bo_unreserve(&buf->tbo);
  787. res = vmw_shader_alloc(dev_priv, buf, size, 0, shader_type);
  788. if (IS_ERR(res)) {
  789. ret = PTR_ERR(res);
  790. goto no_reserve;
  791. }
  792. ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_shader,
  793. vmw_shader_key(user_key, shader_type),
  794. res, list);
  795. vmw_resource_unreference(&res);
  796. no_reserve:
  797. vmw_bo_unreference(&buf);
  798. out:
  799. return ret;
  800. }
  801. /**
  802. * vmw_shader_lookup - Look up a compat shader
  803. *
  804. * @man: Pointer to the command buffer managed resource manager identifying
  805. * the shader namespace.
  806. * @user_key: The user space id of the shader.
  807. * @shader_type: The shader type.
  808. *
  809. * Returns a refcounted pointer to a struct vmw_resource if the shader was
  810. * found. An error pointer otherwise.
  811. */
  812. struct vmw_resource *
  813. vmw_shader_lookup(struct vmw_cmdbuf_res_manager *man,
  814. u32 user_key,
  815. SVGA3dShaderType shader_type)
  816. {
  817. if (!vmw_shader_id_ok(user_key, shader_type))
  818. return ERR_PTR(-EINVAL);
  819. return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_shader,
  820. vmw_shader_key(user_key, shader_type));
  821. }
  822. int vmw_shader_define_ioctl(struct drm_device *dev, void *data,
  823. struct drm_file *file_priv)
  824. {
  825. struct drm_vmw_shader_create_arg *arg =
  826. (struct drm_vmw_shader_create_arg *)data;
  827. return vmw_shader_define(dev, file_priv, arg->shader_type,
  828. arg->buffer_handle,
  829. arg->size, arg->offset,
  830. 0, 0,
  831. &arg->shader_handle);
  832. }