vmwgfx_msg.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  20. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  21. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  23. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <linux/objtool.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/slab.h>
  30. #include <linux/cc_platform.h>
  31. #include <asm/hypervisor.h>
  32. #include <drm/drm_ioctl.h>
  33. #include "vmwgfx_drv.h"
  34. #include "vmwgfx_msg_x86.h"
  35. #include "vmwgfx_msg_arm64.h"
  36. #include "vmwgfx_mksstat.h"
  37. #define MESSAGE_STATUS_SUCCESS 0x0001
  38. #define MESSAGE_STATUS_DORECV 0x0002
  39. #define MESSAGE_STATUS_CPT 0x0010
  40. #define MESSAGE_STATUS_HB 0x0080
  41. #define RPCI_PROTOCOL_NUM 0x49435052
  42. #define GUESTMSG_FLAG_COOKIE 0x80000000
  43. #define RETRIES 3
  44. #define VMW_PORT_CMD_MSG 30
  45. #define VMW_PORT_CMD_HB_MSG 0
  46. #define VMW_PORT_CMD_OPEN_CHANNEL (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
  47. #define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
  48. #define VMW_PORT_CMD_SENDSIZE (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
  49. #define VMW_PORT_CMD_RECVSIZE (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
  50. #define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)
  51. #define VMW_PORT_CMD_MKS_GUEST_STATS 85
  52. #define VMW_PORT_CMD_MKSGS_RESET (0 << 16 | VMW_PORT_CMD_MKS_GUEST_STATS)
  53. #define VMW_PORT_CMD_MKSGS_ADD_PPN (1 << 16 | VMW_PORT_CMD_MKS_GUEST_STATS)
  54. #define VMW_PORT_CMD_MKSGS_REMOVE_PPN (2 << 16 | VMW_PORT_CMD_MKS_GUEST_STATS)
  55. #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
  56. #define MAX_USER_MSG_LENGTH PAGE_SIZE
  57. static u32 vmw_msg_enabled = 1;
  58. enum rpc_msg_type {
  59. MSG_TYPE_OPEN,
  60. MSG_TYPE_SENDSIZE,
  61. MSG_TYPE_SENDPAYLOAD,
  62. MSG_TYPE_RECVSIZE,
  63. MSG_TYPE_RECVPAYLOAD,
  64. MSG_TYPE_RECVSTATUS,
  65. MSG_TYPE_CLOSE,
  66. };
  67. struct rpc_channel {
  68. u16 channel_id;
  69. u32 cookie_high;
  70. u32 cookie_low;
  71. };
  72. #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
  73. /* Kernel mksGuestStats counter names and desciptions; same order as enum mksstat_kern_stats_t */
  74. static const char* const mksstat_kern_name_desc[MKSSTAT_KERN_COUNT][2] =
  75. {
  76. { "vmw_execbuf_ioctl", "vmw_execbuf_ioctl" },
  77. { "vmw_cotable_resize", "vmw_cotable_resize" },
  78. };
  79. #endif
  80. /**
  81. * vmw_open_channel
  82. *
  83. * @channel: RPC channel
  84. * @protocol:
  85. *
  86. * Returns: 0 on success
  87. */
  88. static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
  89. {
  90. u32 ecx, edx, esi, edi;
  91. vmware_hypercall6(VMW_PORT_CMD_OPEN_CHANNEL,
  92. (protocol | GUESTMSG_FLAG_COOKIE), 0,
  93. &ecx, &edx, &esi, &edi);
  94. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  95. return -EINVAL;
  96. channel->channel_id = HIGH_WORD(edx);
  97. channel->cookie_high = esi;
  98. channel->cookie_low = edi;
  99. return 0;
  100. }
  101. /**
  102. * vmw_close_channel
  103. *
  104. * @channel: RPC channel
  105. *
  106. * Returns: 0 on success
  107. */
  108. static int vmw_close_channel(struct rpc_channel *channel)
  109. {
  110. u32 ecx;
  111. vmware_hypercall5(VMW_PORT_CMD_CLOSE_CHANNEL,
  112. 0, channel->channel_id << 16,
  113. channel->cookie_high,
  114. channel->cookie_low,
  115. &ecx);
  116. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  117. return -EINVAL;
  118. return 0;
  119. }
  120. /**
  121. * vmw_port_hb_out - Send the message payload either through the
  122. * high-bandwidth port if available, or through the backdoor otherwise.
  123. * @channel: The rpc channel.
  124. * @msg: NULL-terminated message.
  125. * @hb: Whether the high-bandwidth port is available.
  126. *
  127. * Return: The port status.
  128. */
  129. static unsigned long vmw_port_hb_out(struct rpc_channel *channel,
  130. const char *msg, bool hb)
  131. {
  132. u32 ebx, ecx;
  133. unsigned long msg_len = strlen(msg);
  134. /* HB port can't access encrypted memory. */
  135. if (hb && !cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
  136. vmware_hypercall_hb_out(
  137. (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
  138. msg_len,
  139. channel->channel_id << 16,
  140. (uintptr_t) msg, channel->cookie_low,
  141. channel->cookie_high,
  142. &ebx);
  143. return ebx;
  144. }
  145. /* HB port not available. Send the message 4 bytes at a time. */
  146. ecx = MESSAGE_STATUS_SUCCESS << 16;
  147. while (msg_len && (HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS)) {
  148. unsigned int bytes = min_t(size_t, msg_len, 4);
  149. unsigned long word = 0;
  150. memcpy(&word, msg, bytes);
  151. msg_len -= bytes;
  152. msg += bytes;
  153. vmware_hypercall5(VMW_PORT_CMD_MSG |
  154. (MSG_TYPE_SENDPAYLOAD << 16),
  155. word, channel->channel_id << 16,
  156. channel->cookie_high,
  157. channel->cookie_low,
  158. &ecx);
  159. }
  160. return ecx;
  161. }
  162. /**
  163. * vmw_port_hb_in - Receive the message payload either through the
  164. * high-bandwidth port if available, or through the backdoor otherwise.
  165. * @channel: The rpc channel.
  166. * @reply: Pointer to buffer holding reply.
  167. * @reply_len: Length of the reply.
  168. * @hb: Whether the high-bandwidth port is available.
  169. *
  170. * Return: The port status.
  171. */
  172. static unsigned long vmw_port_hb_in(struct rpc_channel *channel, char *reply,
  173. unsigned long reply_len, bool hb)
  174. {
  175. u32 ebx, ecx, edx;
  176. /* HB port can't access encrypted memory */
  177. if (hb && !cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
  178. vmware_hypercall_hb_in(
  179. (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
  180. reply_len,
  181. channel->channel_id << 16,
  182. channel->cookie_high,
  183. (uintptr_t) reply, channel->cookie_low,
  184. &ebx);
  185. return ebx;
  186. }
  187. /* HB port not available. Retrieve the message 4 bytes at a time. */
  188. ecx = MESSAGE_STATUS_SUCCESS << 16;
  189. while (reply_len) {
  190. unsigned int bytes = min_t(unsigned long, reply_len, 4);
  191. vmware_hypercall7(VMW_PORT_CMD_MSG |
  192. (MSG_TYPE_RECVPAYLOAD << 16),
  193. MESSAGE_STATUS_SUCCESS,
  194. channel->channel_id << 16,
  195. channel->cookie_high,
  196. channel->cookie_low,
  197. &ebx, &ecx, &edx);
  198. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  199. break;
  200. memcpy(reply, &ebx, bytes);
  201. reply_len -= bytes;
  202. reply += bytes;
  203. }
  204. return ecx;
  205. }
  206. /**
  207. * vmw_send_msg: Sends a message to the host
  208. *
  209. * @channel: RPC channel
  210. * @msg: NULL terminated string
  211. *
  212. * Returns: 0 on success
  213. */
  214. static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
  215. {
  216. u32 ebx, ecx;
  217. size_t msg_len = strlen(msg);
  218. int retries = 0;
  219. while (retries < RETRIES) {
  220. retries++;
  221. vmware_hypercall5(VMW_PORT_CMD_SENDSIZE,
  222. msg_len, channel->channel_id << 16,
  223. channel->cookie_high,
  224. channel->cookie_low,
  225. &ecx);
  226. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
  227. /* Expected success. Give up. */
  228. return -EINVAL;
  229. }
  230. /* Send msg */
  231. ebx = vmw_port_hb_out(channel, msg,
  232. !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
  233. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
  234. return 0;
  235. } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
  236. /* A checkpoint occurred. Retry. */
  237. continue;
  238. } else {
  239. break;
  240. }
  241. }
  242. return -EINVAL;
  243. }
  244. STACK_FRAME_NON_STANDARD(vmw_send_msg);
  245. /**
  246. * vmw_recv_msg: Receives a message from the host
  247. *
  248. * Note: It is the caller's responsibility to call kfree() on msg.
  249. *
  250. * @channel: channel opened by vmw_open_channel
  251. * @msg: [OUT] message received from the host
  252. * @msg_len: message length
  253. */
  254. static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
  255. size_t *msg_len)
  256. {
  257. u32 ebx, ecx, edx;
  258. char *reply;
  259. size_t reply_len;
  260. int retries = 0;
  261. *msg_len = 0;
  262. *msg = NULL;
  263. while (retries < RETRIES) {
  264. retries++;
  265. vmware_hypercall7(VMW_PORT_CMD_RECVSIZE,
  266. 0, channel->channel_id << 16,
  267. channel->cookie_high,
  268. channel->cookie_low,
  269. &ebx, &ecx, &edx);
  270. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
  271. DRM_ERROR("Failed to get reply size for host message.\n");
  272. return -EINVAL;
  273. }
  274. /* No reply available. This is okay. */
  275. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
  276. return 0;
  277. reply_len = ebx;
  278. reply = kzalloc(reply_len + 1, GFP_KERNEL);
  279. if (!reply) {
  280. DRM_ERROR("Cannot allocate memory for host message reply.\n");
  281. return -ENOMEM;
  282. }
  283. /* Receive buffer */
  284. ebx = vmw_port_hb_in(channel, reply, reply_len,
  285. !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
  286. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
  287. kfree(reply);
  288. reply = NULL;
  289. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
  290. /* A checkpoint occurred. Retry. */
  291. continue;
  292. }
  293. return -EINVAL;
  294. }
  295. reply[reply_len] = '\0';
  296. vmware_hypercall5(VMW_PORT_CMD_RECVSTATUS,
  297. MESSAGE_STATUS_SUCCESS,
  298. channel->channel_id << 16,
  299. channel->cookie_high,
  300. channel->cookie_low,
  301. &ecx);
  302. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
  303. kfree(reply);
  304. reply = NULL;
  305. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
  306. /* A checkpoint occurred. Retry. */
  307. continue;
  308. }
  309. return -EINVAL;
  310. }
  311. break;
  312. }
  313. if (!reply)
  314. return -EINVAL;
  315. *msg_len = reply_len;
  316. *msg = reply;
  317. return 0;
  318. }
  319. STACK_FRAME_NON_STANDARD(vmw_recv_msg);
  320. /**
  321. * vmw_host_get_guestinfo: Gets a GuestInfo parameter
  322. *
  323. * Gets the value of a GuestInfo.* parameter. The value returned will be in
  324. * a string, and it is up to the caller to post-process.
  325. *
  326. * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
  327. * @buffer: if NULL, *reply_len will contain reply size.
  328. * @length: size of the reply_buf. Set to size of reply upon return
  329. *
  330. * Returns: 0 on success
  331. */
  332. int vmw_host_get_guestinfo(const char *guest_info_param,
  333. char *buffer, size_t *length)
  334. {
  335. struct rpc_channel channel;
  336. char *msg, *reply = NULL;
  337. size_t reply_len = 0;
  338. if (!vmw_msg_enabled)
  339. return -ENODEV;
  340. if (!guest_info_param || !length)
  341. return -EINVAL;
  342. msg = kasprintf(GFP_KERNEL, "info-get %s", guest_info_param);
  343. if (!msg) {
  344. DRM_ERROR("Cannot allocate memory to get guest info \"%s\".",
  345. guest_info_param);
  346. return -ENOMEM;
  347. }
  348. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
  349. goto out_open;
  350. if (vmw_send_msg(&channel, msg) ||
  351. vmw_recv_msg(&channel, (void *) &reply, &reply_len))
  352. goto out_msg;
  353. vmw_close_channel(&channel);
  354. if (buffer && reply && reply_len > 0) {
  355. /* Remove reply code, which are the first 2 characters of
  356. * the reply
  357. */
  358. reply_len = max(reply_len - 2, (size_t) 0);
  359. reply_len = min(reply_len, *length);
  360. if (reply_len > 0)
  361. memcpy(buffer, reply + 2, reply_len);
  362. }
  363. *length = reply_len;
  364. kfree(reply);
  365. kfree(msg);
  366. return 0;
  367. out_msg:
  368. vmw_close_channel(&channel);
  369. kfree(reply);
  370. out_open:
  371. *length = 0;
  372. kfree(msg);
  373. DRM_ERROR("Failed to get guest info \"%s\".", guest_info_param);
  374. return -EINVAL;
  375. }
  376. /**
  377. * vmw_host_printf: Sends a log message to the host
  378. *
  379. * @fmt: Regular printf format string and arguments
  380. *
  381. * Returns: 0 on success
  382. */
  383. __printf(1, 2)
  384. int vmw_host_printf(const char *fmt, ...)
  385. {
  386. va_list ap;
  387. struct rpc_channel channel;
  388. char *msg;
  389. char *log;
  390. int ret = 0;
  391. if (!vmw_msg_enabled)
  392. return -ENODEV;
  393. if (!fmt)
  394. return ret;
  395. va_start(ap, fmt);
  396. log = kvasprintf(GFP_KERNEL, fmt, ap);
  397. va_end(ap);
  398. if (!log) {
  399. DRM_ERROR("Cannot allocate memory for the log message.\n");
  400. return -ENOMEM;
  401. }
  402. msg = kasprintf(GFP_KERNEL, "log %s", log);
  403. if (!msg) {
  404. DRM_ERROR("Cannot allocate memory for host log message.\n");
  405. kfree(log);
  406. return -ENOMEM;
  407. }
  408. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
  409. goto out_open;
  410. if (vmw_send_msg(&channel, msg))
  411. goto out_msg;
  412. vmw_close_channel(&channel);
  413. kfree(msg);
  414. kfree(log);
  415. return 0;
  416. out_msg:
  417. vmw_close_channel(&channel);
  418. out_open:
  419. kfree(msg);
  420. kfree(log);
  421. DRM_ERROR("Failed to send host log message.\n");
  422. return -EINVAL;
  423. }
  424. /**
  425. * vmw_msg_ioctl: Sends and receveives a message to/from host from/to user-space
  426. *
  427. * Sends a message from user-space to host.
  428. * Can also receive a result from host and return that to user-space.
  429. *
  430. * @dev: Identifies the drm device.
  431. * @data: Pointer to the ioctl argument.
  432. * @file_priv: Identifies the caller.
  433. * Return: Zero on success, negative error code on error.
  434. */
  435. int vmw_msg_ioctl(struct drm_device *dev, void *data,
  436. struct drm_file *file_priv)
  437. {
  438. struct drm_vmw_msg_arg *arg =
  439. (struct drm_vmw_msg_arg *)data;
  440. struct rpc_channel channel;
  441. char *msg;
  442. int length;
  443. msg = kmalloc(MAX_USER_MSG_LENGTH, GFP_KERNEL);
  444. if (!msg) {
  445. DRM_ERROR("Cannot allocate memory for log message.\n");
  446. return -ENOMEM;
  447. }
  448. length = strncpy_from_user(msg, (void __user *)((unsigned long)arg->send),
  449. MAX_USER_MSG_LENGTH);
  450. if (length < 0 || length >= MAX_USER_MSG_LENGTH) {
  451. DRM_ERROR("Userspace message access failure.\n");
  452. kfree(msg);
  453. return -EINVAL;
  454. }
  455. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM)) {
  456. DRM_ERROR("Failed to open channel.\n");
  457. goto out_open;
  458. }
  459. if (vmw_send_msg(&channel, msg)) {
  460. DRM_ERROR("Failed to send message to host.\n");
  461. goto out_msg;
  462. }
  463. if (!arg->send_only) {
  464. char *reply = NULL;
  465. size_t reply_len = 0;
  466. if (vmw_recv_msg(&channel, (void *) &reply, &reply_len)) {
  467. DRM_ERROR("Failed to receive message from host.\n");
  468. goto out_msg;
  469. }
  470. if (reply && reply_len > 0) {
  471. if (copy_to_user((void __user *)((unsigned long)arg->receive),
  472. reply, reply_len)) {
  473. DRM_ERROR("Failed to copy message to userspace.\n");
  474. kfree(reply);
  475. goto out_msg;
  476. }
  477. arg->receive_len = (__u32)reply_len;
  478. }
  479. kfree(reply);
  480. }
  481. vmw_close_channel(&channel);
  482. kfree(msg);
  483. return 0;
  484. out_msg:
  485. vmw_close_channel(&channel);
  486. out_open:
  487. kfree(msg);
  488. return -EINVAL;
  489. }
  490. /**
  491. * reset_ppn_array: Resets a PPN64 array to INVALID_PPN64 content
  492. *
  493. * @arr: Array to reset.
  494. * @size: Array length.
  495. */
  496. static inline void reset_ppn_array(PPN64 *arr, size_t size)
  497. {
  498. size_t i;
  499. BUG_ON(!arr || size == 0);
  500. for (i = 0; i < size; ++i)
  501. arr[i] = INVALID_PPN64;
  502. }
  503. /**
  504. * hypervisor_ppn_reset_all: Removes all mksGuestStat instance descriptors from
  505. * the hypervisor. All related pages should be subsequently unpinned or freed.
  506. *
  507. */
  508. static inline void hypervisor_ppn_reset_all(void)
  509. {
  510. vmware_hypercall1(VMW_PORT_CMD_MKSGS_RESET, 0);
  511. }
  512. /**
  513. * hypervisor_ppn_add: Adds a single mksGuestStat instance descriptor to the
  514. * hypervisor. Any related userspace pages should be pinned in advance.
  515. *
  516. * @pfn: Physical page number of the instance descriptor
  517. */
  518. static inline void hypervisor_ppn_add(PPN64 pfn)
  519. {
  520. vmware_hypercall1(VMW_PORT_CMD_MKSGS_ADD_PPN, (unsigned long)pfn);
  521. }
  522. /**
  523. * hypervisor_ppn_remove: Removes a single mksGuestStat instance descriptor from
  524. * the hypervisor. All related pages should be subsequently unpinned or freed.
  525. *
  526. * @pfn: Physical page number of the instance descriptor
  527. */
  528. static inline void hypervisor_ppn_remove(PPN64 pfn)
  529. {
  530. vmware_hypercall1(VMW_PORT_CMD_MKSGS_REMOVE_PPN, (unsigned long)pfn);
  531. }
  532. #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
  533. /* Order of the total number of pages used for kernel-internal mksGuestStat; at least 2 */
  534. #define MKSSTAT_KERNEL_PAGES_ORDER 2
  535. /* Header to the text description of mksGuestStat instance descriptor */
  536. #define MKSSTAT_KERNEL_DESCRIPTION "vmwgfx"
  537. /**
  538. * mksstat_init_record_time: Initializes an MKSGuestStatCounterTime-based record
  539. * for the respective mksGuestStat index.
  540. *
  541. * @stat_idx: Index of the MKSGuestStatCounterTime-based mksGuestStat record.
  542. * @pstat: Pointer to array of MKSGuestStatCounterTime.
  543. * @pinfo: Pointer to array of MKSGuestStatInfoEntry.
  544. * @pstrs: Pointer to current end of the name/description sequence.
  545. * Return: Pointer to the new end of the names/description sequence.
  546. */
  547. static inline char *mksstat_init_record_time(mksstat_kern_stats_t stat_idx,
  548. MKSGuestStatCounterTime *pstat, MKSGuestStatInfoEntry *pinfo, char *pstrs)
  549. {
  550. char *const pstrd = pstrs + strlen(mksstat_kern_name_desc[stat_idx][0]) + 1;
  551. strcpy(pstrs, mksstat_kern_name_desc[stat_idx][0]);
  552. strcpy(pstrd, mksstat_kern_name_desc[stat_idx][1]);
  553. pinfo[stat_idx].name.s = pstrs;
  554. pinfo[stat_idx].description.s = pstrd;
  555. pinfo[stat_idx].flags = MKS_GUEST_STAT_FLAG_TIME;
  556. pinfo[stat_idx].stat.counterTime = &pstat[stat_idx];
  557. return pstrd + strlen(mksstat_kern_name_desc[stat_idx][1]) + 1;
  558. }
  559. /**
  560. * mksstat_init_kern_id: Creates a single mksGuestStat instance descriptor and
  561. * kernel-internal counters. Adds PFN mapping to the hypervisor.
  562. *
  563. * Create a single mksGuestStat instance descriptor and corresponding structures
  564. * for all kernel-internal counters. The corresponding PFNs are mapped with the
  565. * hypervisor.
  566. *
  567. * @ppage: Output pointer to page containing the instance descriptor.
  568. * Return: Zero on success, negative error code on error.
  569. */
  570. static int mksstat_init_kern_id(struct page **ppage)
  571. {
  572. MKSGuestStatInstanceDescriptor *pdesc;
  573. MKSGuestStatCounterTime *pstat;
  574. MKSGuestStatInfoEntry *pinfo;
  575. char *pstrs, *pstrs_acc;
  576. /* Allocate pages for the kernel-internal instance descriptor */
  577. struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, MKSSTAT_KERNEL_PAGES_ORDER);
  578. if (!page)
  579. return -ENOMEM;
  580. pdesc = page_address(page);
  581. pstat = vmw_mksstat_get_kern_pstat(pdesc);
  582. pinfo = vmw_mksstat_get_kern_pinfo(pdesc);
  583. pstrs = vmw_mksstat_get_kern_pstrs(pdesc);
  584. /* Set up all kernel-internal counters and corresponding structures */
  585. pstrs_acc = pstrs;
  586. pstrs_acc = mksstat_init_record_time(MKSSTAT_KERN_EXECBUF, pstat, pinfo, pstrs_acc);
  587. pstrs_acc = mksstat_init_record_time(MKSSTAT_KERN_COTABLE_RESIZE, pstat, pinfo, pstrs_acc);
  588. /* Add new counters above, in their order of appearance in mksstat_kern_stats_t */
  589. BUG_ON(pstrs_acc - pstrs > PAGE_SIZE);
  590. /* Set up the kernel-internal instance descriptor */
  591. pdesc->reservedMBZ = 0;
  592. pdesc->statStartVA = (uintptr_t)pstat;
  593. pdesc->strsStartVA = (uintptr_t)pstrs;
  594. pdesc->statLength = sizeof(*pstat) * MKSSTAT_KERN_COUNT;
  595. pdesc->infoLength = sizeof(*pinfo) * MKSSTAT_KERN_COUNT;
  596. pdesc->strsLength = pstrs_acc - pstrs;
  597. snprintf(pdesc->description, ARRAY_SIZE(pdesc->description) - 1, "%s pid=%d",
  598. MKSSTAT_KERNEL_DESCRIPTION, current->pid);
  599. pdesc->statPPNs[0] = page_to_pfn(virt_to_page(pstat));
  600. reset_ppn_array(pdesc->statPPNs + 1, ARRAY_SIZE(pdesc->statPPNs) - 1);
  601. pdesc->infoPPNs[0] = page_to_pfn(virt_to_page(pinfo));
  602. reset_ppn_array(pdesc->infoPPNs + 1, ARRAY_SIZE(pdesc->infoPPNs) - 1);
  603. pdesc->strsPPNs[0] = page_to_pfn(virt_to_page(pstrs));
  604. reset_ppn_array(pdesc->strsPPNs + 1, ARRAY_SIZE(pdesc->strsPPNs) - 1);
  605. *ppage = page;
  606. hypervisor_ppn_add((PPN64)page_to_pfn(page));
  607. return 0;
  608. }
  609. /**
  610. * vmw_mksstat_get_kern_slot: Acquires a slot for a single kernel-internal
  611. * mksGuestStat instance descriptor.
  612. *
  613. * Find a slot for a single kernel-internal mksGuestStat instance descriptor.
  614. * In case no such was already present, allocate a new one and set up a kernel-
  615. * internal mksGuestStat instance descriptor for the former.
  616. *
  617. * @pid: Process for which a slot is sought.
  618. * @dev_priv: Identifies the drm private device.
  619. * Return: Non-negative slot on success, negative error code on error.
  620. */
  621. int vmw_mksstat_get_kern_slot(pid_t pid, struct vmw_private *dev_priv)
  622. {
  623. const size_t base = (u32)hash_32(pid, MKSSTAT_CAPACITY_LOG2);
  624. size_t i;
  625. for (i = 0; i < ARRAY_SIZE(dev_priv->mksstat_kern_pids); ++i) {
  626. const size_t slot = (i + base) % ARRAY_SIZE(dev_priv->mksstat_kern_pids);
  627. /* Check if an instance descriptor for this pid is already present */
  628. if (pid == (pid_t)atomic_read(&dev_priv->mksstat_kern_pids[slot]))
  629. return (int)slot;
  630. /* Set up a new instance descriptor for this pid */
  631. if (!atomic_cmpxchg(&dev_priv->mksstat_kern_pids[slot], 0, MKSSTAT_PID_RESERVED)) {
  632. const int ret = mksstat_init_kern_id(&dev_priv->mksstat_kern_pages[slot]);
  633. if (!ret) {
  634. /* Reset top-timer tracking for this slot */
  635. dev_priv->mksstat_kern_top_timer[slot] = MKSSTAT_KERN_COUNT;
  636. atomic_set(&dev_priv->mksstat_kern_pids[slot], pid);
  637. return (int)slot;
  638. }
  639. atomic_set(&dev_priv->mksstat_kern_pids[slot], 0);
  640. return ret;
  641. }
  642. }
  643. return -ENOSPC;
  644. }
  645. #endif
  646. /**
  647. * vmw_mksstat_cleanup_descriptor: Frees a single userspace-originating
  648. * mksGuestStat instance-descriptor page and unpins all related user pages.
  649. *
  650. * Unpin all user pages realated to this instance descriptor and free
  651. * the instance-descriptor page itself.
  652. *
  653. * @page: Page of the instance descriptor.
  654. */
  655. static void vmw_mksstat_cleanup_descriptor(struct page *page)
  656. {
  657. MKSGuestStatInstanceDescriptor *pdesc = page_address(page);
  658. size_t i;
  659. for (i = 0; i < ARRAY_SIZE(pdesc->statPPNs) && pdesc->statPPNs[i] != INVALID_PPN64; ++i)
  660. unpin_user_page(pfn_to_page(pdesc->statPPNs[i]));
  661. for (i = 0; i < ARRAY_SIZE(pdesc->infoPPNs) && pdesc->infoPPNs[i] != INVALID_PPN64; ++i)
  662. unpin_user_page(pfn_to_page(pdesc->infoPPNs[i]));
  663. for (i = 0; i < ARRAY_SIZE(pdesc->strsPPNs) && pdesc->strsPPNs[i] != INVALID_PPN64; ++i)
  664. unpin_user_page(pfn_to_page(pdesc->strsPPNs[i]));
  665. __free_page(page);
  666. }
  667. /**
  668. * vmw_mksstat_remove_all: Resets all mksGuestStat instance descriptors
  669. * from the hypervisor.
  670. *
  671. * Discard all hypervisor PFN mappings, containing active mksGuestState instance
  672. * descriptors, unpin the related userspace pages and free the related kernel pages.
  673. *
  674. * @dev_priv: Identifies the drm private device.
  675. * Return: Zero on success, negative error code on error.
  676. */
  677. int vmw_mksstat_remove_all(struct vmw_private *dev_priv)
  678. {
  679. int ret = 0;
  680. size_t i;
  681. /* Discard all PFN mappings with the hypervisor */
  682. hypervisor_ppn_reset_all();
  683. /* Discard all userspace-originating instance descriptors and unpin all related pages */
  684. for (i = 0; i < ARRAY_SIZE(dev_priv->mksstat_user_pids); ++i) {
  685. const pid_t pid0 = (pid_t)atomic_read(&dev_priv->mksstat_user_pids[i]);
  686. if (!pid0)
  687. continue;
  688. if (pid0 != MKSSTAT_PID_RESERVED) {
  689. const pid_t pid1 = atomic_cmpxchg(&dev_priv->mksstat_user_pids[i], pid0, MKSSTAT_PID_RESERVED);
  690. if (!pid1)
  691. continue;
  692. if (pid1 == pid0) {
  693. struct page *const page = dev_priv->mksstat_user_pages[i];
  694. BUG_ON(!page);
  695. dev_priv->mksstat_user_pages[i] = NULL;
  696. atomic_set(&dev_priv->mksstat_user_pids[i], 0);
  697. vmw_mksstat_cleanup_descriptor(page);
  698. continue;
  699. }
  700. }
  701. ret = -EAGAIN;
  702. }
  703. #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
  704. /* Discard all kernel-internal instance descriptors and free all related pages */
  705. for (i = 0; i < ARRAY_SIZE(dev_priv->mksstat_kern_pids); ++i) {
  706. const pid_t pid0 = (pid_t)atomic_read(&dev_priv->mksstat_kern_pids[i]);
  707. if (!pid0)
  708. continue;
  709. if (pid0 != MKSSTAT_PID_RESERVED) {
  710. const pid_t pid1 = atomic_cmpxchg(&dev_priv->mksstat_kern_pids[i], pid0, MKSSTAT_PID_RESERVED);
  711. if (!pid1)
  712. continue;
  713. if (pid1 == pid0) {
  714. struct page *const page = dev_priv->mksstat_kern_pages[i];
  715. BUG_ON(!page);
  716. dev_priv->mksstat_kern_pages[i] = NULL;
  717. atomic_set(&dev_priv->mksstat_kern_pids[i], 0);
  718. __free_pages(page, MKSSTAT_KERNEL_PAGES_ORDER);
  719. continue;
  720. }
  721. }
  722. ret = -EAGAIN;
  723. }
  724. #endif
  725. return ret;
  726. }
  727. /**
  728. * vmw_mksstat_reset_ioctl: Resets all mksGuestStat instance descriptors
  729. * from the hypervisor.
  730. *
  731. * Discard all hypervisor PFN mappings, containing active mksGuestStat instance
  732. * descriptors, unpin the related userspace pages and free the related kernel pages.
  733. *
  734. * @dev: Identifies the drm device.
  735. * @data: Pointer to the ioctl argument.
  736. * @file_priv: Identifies the caller; unused.
  737. * Return: Zero on success, negative error code on error.
  738. */
  739. int vmw_mksstat_reset_ioctl(struct drm_device *dev, void *data,
  740. struct drm_file *file_priv)
  741. {
  742. struct vmw_private *const dev_priv = vmw_priv(dev);
  743. return vmw_mksstat_remove_all(dev_priv);
  744. }
  745. /**
  746. * vmw_mksstat_add_ioctl: Creates a single userspace-originating mksGuestStat
  747. * instance descriptor and registers that with the hypervisor.
  748. *
  749. * Create a hypervisor PFN mapping, containing a single mksGuestStat instance
  750. * descriptor and pin the corresponding userspace pages.
  751. *
  752. * @dev: Identifies the drm device.
  753. * @data: Pointer to the ioctl argument.
  754. * @file_priv: Identifies the caller; unused.
  755. * Return: Zero on success, negative error code on error.
  756. */
  757. int vmw_mksstat_add_ioctl(struct drm_device *dev, void *data,
  758. struct drm_file *file_priv)
  759. {
  760. struct drm_vmw_mksstat_add_arg *arg =
  761. (struct drm_vmw_mksstat_add_arg *) data;
  762. struct vmw_private *const dev_priv = vmw_priv(dev);
  763. const size_t num_pages_stat = PFN_UP(arg->stat_len);
  764. const size_t num_pages_info = PFN_UP(arg->info_len);
  765. const size_t num_pages_strs = PFN_UP(arg->strs_len);
  766. long desc_len;
  767. long nr_pinned_stat;
  768. long nr_pinned_info;
  769. long nr_pinned_strs;
  770. MKSGuestStatInstanceDescriptor *pdesc;
  771. struct page *page = NULL;
  772. struct page **pages_stat = NULL;
  773. struct page **pages_info = NULL;
  774. struct page **pages_strs = NULL;
  775. size_t i, slot;
  776. int ret_err = -ENOMEM;
  777. arg->id = -1;
  778. if (!arg->stat || !arg->info || !arg->strs)
  779. return -EINVAL;
  780. if (!arg->stat_len || !arg->info_len || !arg->strs_len)
  781. return -EINVAL;
  782. if (!arg->description)
  783. return -EINVAL;
  784. if (num_pages_stat > ARRAY_SIZE(pdesc->statPPNs) ||
  785. num_pages_info > ARRAY_SIZE(pdesc->infoPPNs) ||
  786. num_pages_strs > ARRAY_SIZE(pdesc->strsPPNs))
  787. return -EINVAL;
  788. /* Find an available slot in the mksGuestStats user array and reserve it */
  789. for (slot = 0; slot < ARRAY_SIZE(dev_priv->mksstat_user_pids); ++slot)
  790. if (!atomic_cmpxchg(&dev_priv->mksstat_user_pids[slot], 0, MKSSTAT_PID_RESERVED))
  791. break;
  792. if (slot == ARRAY_SIZE(dev_priv->mksstat_user_pids))
  793. return -ENOSPC;
  794. BUG_ON(dev_priv->mksstat_user_pages[slot]);
  795. /* Allocate statically-sized temp arrays for pages -- too big to keep in frame */
  796. pages_stat = (struct page **)kmalloc_array(
  797. ARRAY_SIZE(pdesc->statPPNs) +
  798. ARRAY_SIZE(pdesc->infoPPNs) +
  799. ARRAY_SIZE(pdesc->strsPPNs), sizeof(*pages_stat), GFP_KERNEL);
  800. if (!pages_stat)
  801. goto err_nomem;
  802. pages_info = pages_stat + ARRAY_SIZE(pdesc->statPPNs);
  803. pages_strs = pages_info + ARRAY_SIZE(pdesc->infoPPNs);
  804. /* Allocate a page for the instance descriptor */
  805. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  806. if (!page)
  807. goto err_nomem;
  808. /* Set up the instance descriptor */
  809. pdesc = page_address(page);
  810. pdesc->reservedMBZ = 0;
  811. pdesc->statStartVA = arg->stat;
  812. pdesc->strsStartVA = arg->strs;
  813. pdesc->statLength = arg->stat_len;
  814. pdesc->infoLength = arg->info_len;
  815. pdesc->strsLength = arg->strs_len;
  816. desc_len = strncpy_from_user(pdesc->description, u64_to_user_ptr(arg->description),
  817. ARRAY_SIZE(pdesc->description) - 1);
  818. if (desc_len < 0) {
  819. ret_err = -EFAULT;
  820. goto err_nomem;
  821. }
  822. reset_ppn_array(pdesc->statPPNs, ARRAY_SIZE(pdesc->statPPNs));
  823. reset_ppn_array(pdesc->infoPPNs, ARRAY_SIZE(pdesc->infoPPNs));
  824. reset_ppn_array(pdesc->strsPPNs, ARRAY_SIZE(pdesc->strsPPNs));
  825. /* Pin mksGuestStat user pages and store those in the instance descriptor */
  826. nr_pinned_stat = pin_user_pages_fast(arg->stat, num_pages_stat, FOLL_LONGTERM, pages_stat);
  827. if (num_pages_stat != nr_pinned_stat)
  828. goto err_pin_stat;
  829. for (i = 0; i < num_pages_stat; ++i)
  830. pdesc->statPPNs[i] = page_to_pfn(pages_stat[i]);
  831. nr_pinned_info = pin_user_pages_fast(arg->info, num_pages_info, FOLL_LONGTERM, pages_info);
  832. if (num_pages_info != nr_pinned_info)
  833. goto err_pin_info;
  834. for (i = 0; i < num_pages_info; ++i)
  835. pdesc->infoPPNs[i] = page_to_pfn(pages_info[i]);
  836. nr_pinned_strs = pin_user_pages_fast(arg->strs, num_pages_strs, FOLL_LONGTERM, pages_strs);
  837. if (num_pages_strs != nr_pinned_strs)
  838. goto err_pin_strs;
  839. for (i = 0; i < num_pages_strs; ++i)
  840. pdesc->strsPPNs[i] = page_to_pfn(pages_strs[i]);
  841. /* Send the descriptor to the host via a hypervisor call. The mksGuestStat
  842. pages will remain in use until the user requests a matching remove stats
  843. or a stats reset occurs. */
  844. hypervisor_ppn_add((PPN64)page_to_pfn(page));
  845. dev_priv->mksstat_user_pages[slot] = page;
  846. atomic_set(&dev_priv->mksstat_user_pids[slot], task_pgrp_vnr(current));
  847. arg->id = slot;
  848. DRM_DEV_INFO(dev->dev, "pid=%d arg.description='%.*s' id=%zu\n", current->pid, (int)desc_len, pdesc->description, slot);
  849. kfree(pages_stat);
  850. return 0;
  851. err_pin_strs:
  852. if (nr_pinned_strs > 0)
  853. unpin_user_pages(pages_strs, nr_pinned_strs);
  854. err_pin_info:
  855. if (nr_pinned_info > 0)
  856. unpin_user_pages(pages_info, nr_pinned_info);
  857. err_pin_stat:
  858. if (nr_pinned_stat > 0)
  859. unpin_user_pages(pages_stat, nr_pinned_stat);
  860. err_nomem:
  861. atomic_set(&dev_priv->mksstat_user_pids[slot], 0);
  862. if (page)
  863. __free_page(page);
  864. kfree(pages_stat);
  865. return ret_err;
  866. }
  867. /**
  868. * vmw_mksstat_remove_ioctl: Removes a single userspace-originating mksGuestStat
  869. * instance descriptor from the hypervisor.
  870. *
  871. * Discard a hypervisor PFN mapping, containing a single mksGuestStat instance
  872. * descriptor and unpin the corresponding userspace pages.
  873. *
  874. * @dev: Identifies the drm device.
  875. * @data: Pointer to the ioctl argument.
  876. * @file_priv: Identifies the caller; unused.
  877. * Return: Zero on success, negative error code on error.
  878. */
  879. int vmw_mksstat_remove_ioctl(struct drm_device *dev, void *data,
  880. struct drm_file *file_priv)
  881. {
  882. struct drm_vmw_mksstat_remove_arg *arg =
  883. (struct drm_vmw_mksstat_remove_arg *) data;
  884. struct vmw_private *const dev_priv = vmw_priv(dev);
  885. const size_t slot = arg->id;
  886. pid_t pgid, pid;
  887. if (slot >= ARRAY_SIZE(dev_priv->mksstat_user_pids))
  888. return -EINVAL;
  889. DRM_DEV_INFO(dev->dev, "pid=%d arg.id=%zu\n", current->pid, slot);
  890. pgid = task_pgrp_vnr(current);
  891. pid = atomic_cmpxchg(&dev_priv->mksstat_user_pids[slot], pgid, MKSSTAT_PID_RESERVED);
  892. if (!pid)
  893. return 0;
  894. if (pid == pgid) {
  895. struct page *const page = dev_priv->mksstat_user_pages[slot];
  896. BUG_ON(!page);
  897. dev_priv->mksstat_user_pages[slot] = NULL;
  898. atomic_set(&dev_priv->mksstat_user_pids[slot], 0);
  899. hypervisor_ppn_remove((PPN64)page_to_pfn(page));
  900. vmw_mksstat_cleanup_descriptor(page);
  901. return 0;
  902. }
  903. return -EAGAIN;
  904. }
  905. /**
  906. * vmw_disable_backdoor: Disables all backdoor communication
  907. * with the hypervisor.
  908. */
  909. void vmw_disable_backdoor(void)
  910. {
  911. vmw_msg_enabled = 0;
  912. }