wbrf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Wifi Frequency Band Manage Interface
  4. * Copyright (C) 2023 Advanced Micro Devices
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/acpi_amd_wbrf.h>
  8. /*
  9. * Functions bit vector for WBRF method
  10. *
  11. * Bit 0: WBRF supported.
  12. * Bit 1: Function 1 (Add / Remove frequency) is supported.
  13. * Bit 2: Function 2 (Get frequency list) is supported.
  14. */
  15. #define WBRF_ENABLED 0x0
  16. #define WBRF_RECORD 0x1
  17. #define WBRF_RETRIEVE 0x2
  18. #define WBRF_REVISION 0x1
  19. /*
  20. * The data structure used for WBRF_RETRIEVE is not naturally aligned.
  21. * And unfortunately the design has been settled down.
  22. */
  23. struct amd_wbrf_ranges_out {
  24. u32 num_of_ranges;
  25. struct freq_band_range band_list[MAX_NUM_OF_WBRF_RANGES];
  26. } __packed;
  27. static const guid_t wifi_acpi_dsm_guid =
  28. GUID_INIT(0x7b7656cf, 0xdc3d, 0x4c1c,
  29. 0x83, 0xe9, 0x66, 0xe7, 0x21, 0xde, 0x30, 0x70);
  30. /*
  31. * Used to notify consumer (amdgpu driver currently) about
  32. * the wifi frequency is change.
  33. */
  34. static BLOCKING_NOTIFIER_HEAD(wbrf_chain_head);
  35. static int wbrf_record(struct acpi_device *adev, uint8_t action, struct wbrf_ranges_in_out *in)
  36. {
  37. union acpi_object argv4;
  38. u32 num_of_ranges = 0;
  39. u32 num_of_elements;
  40. u32 arg_idx = 0;
  41. int ret;
  42. u32 i;
  43. if (!in)
  44. return -EINVAL;
  45. for (i = 0; i < ARRAY_SIZE(in->band_list); i++) {
  46. if (in->band_list[i].start && in->band_list[i].end)
  47. num_of_ranges++;
  48. }
  49. /*
  50. * The num_of_ranges value in the "in" object supplied by
  51. * the caller is required to be equal to the number of
  52. * entries in the band_list array in there.
  53. */
  54. if (num_of_ranges != in->num_of_ranges)
  55. return -EINVAL;
  56. /*
  57. * Every input frequency band comes with two end points(start/end)
  58. * and each is accounted as an element. Meanwhile the range count
  59. * and action type are accounted as an element each.
  60. * So, the total element count = 2 * num_of_ranges + 1 + 1.
  61. */
  62. num_of_elements = 2 * num_of_ranges + 2;
  63. union acpi_object *tmp __free(kfree) = kzalloc_objs(*tmp,
  64. num_of_elements);
  65. if (!tmp)
  66. return -ENOMEM;
  67. argv4.package.type = ACPI_TYPE_PACKAGE;
  68. argv4.package.count = num_of_elements;
  69. argv4.package.elements = tmp;
  70. /* save the number of ranges*/
  71. tmp[0].integer.type = ACPI_TYPE_INTEGER;
  72. tmp[0].integer.value = num_of_ranges;
  73. /* save the action(WBRF_RECORD_ADD/REMOVE/RETRIEVE) */
  74. tmp[1].integer.type = ACPI_TYPE_INTEGER;
  75. tmp[1].integer.value = action;
  76. arg_idx = 2;
  77. for (i = 0; i < ARRAY_SIZE(in->band_list); i++) {
  78. if (!in->band_list[i].start || !in->band_list[i].end)
  79. continue;
  80. tmp[arg_idx].integer.type = ACPI_TYPE_INTEGER;
  81. tmp[arg_idx++].integer.value = in->band_list[i].start;
  82. tmp[arg_idx].integer.type = ACPI_TYPE_INTEGER;
  83. tmp[arg_idx++].integer.value = in->band_list[i].end;
  84. }
  85. union acpi_object *obj __free(kfree) =
  86. acpi_evaluate_dsm(adev->handle, &wifi_acpi_dsm_guid,
  87. WBRF_REVISION, WBRF_RECORD, &argv4);
  88. if (!obj)
  89. return -EINVAL;
  90. if (obj->type != ACPI_TYPE_INTEGER)
  91. return -EINVAL;
  92. ret = obj->integer.value;
  93. if (ret)
  94. return -EINVAL;
  95. return ret;
  96. }
  97. /**
  98. * acpi_amd_wbrf_add_remove - add or remove the frequency band the device is using
  99. *
  100. * @dev: device pointer
  101. * @action: remove or add the frequency band into bios
  102. * @in: input structure containing the frequency band the device is using
  103. *
  104. * Broadcast to other consumers the frequency band the device starts
  105. * to use. Underneath the surface the information is cached into an
  106. * internal buffer first. Then a notification is sent to all those
  107. * registered consumers. So then they can retrieve that buffer to
  108. * know the latest active frequency bands. Consumers that haven't
  109. * yet been registered can retrieve the information from the cache
  110. * when they register.
  111. *
  112. * Return:
  113. * 0 for success add/remove wifi frequency band.
  114. * Returns a negative error code for failure.
  115. */
  116. int acpi_amd_wbrf_add_remove(struct device *dev, uint8_t action, struct wbrf_ranges_in_out *in)
  117. {
  118. struct acpi_device *adev;
  119. int ret;
  120. adev = ACPI_COMPANION(dev);
  121. if (!adev)
  122. return -ENODEV;
  123. ret = wbrf_record(adev, action, in);
  124. if (ret)
  125. return ret;
  126. blocking_notifier_call_chain(&wbrf_chain_head, WBRF_CHANGED, NULL);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_GPL(acpi_amd_wbrf_add_remove);
  130. /**
  131. * acpi_amd_wbrf_supported_producer - determine if the WBRF can be enabled
  132. * for the device as a producer
  133. *
  134. * @dev: device pointer
  135. *
  136. * Check if the platform equipped with necessary implementations to
  137. * support WBRF for the device as a producer.
  138. *
  139. * Return:
  140. * true if WBRF is supported, otherwise returns false
  141. */
  142. bool acpi_amd_wbrf_supported_producer(struct device *dev)
  143. {
  144. struct acpi_device *adev;
  145. adev = ACPI_COMPANION(dev);
  146. if (!adev)
  147. return false;
  148. return acpi_check_dsm(adev->handle, &wifi_acpi_dsm_guid,
  149. WBRF_REVISION, BIT(WBRF_RECORD));
  150. }
  151. EXPORT_SYMBOL_GPL(acpi_amd_wbrf_supported_producer);
  152. /**
  153. * acpi_amd_wbrf_supported_consumer - determine if the WBRF can be enabled
  154. * for the device as a consumer
  155. *
  156. * @dev: device pointer
  157. *
  158. * Determine if the platform equipped with necessary implementations to
  159. * support WBRF for the device as a consumer.
  160. *
  161. * Return:
  162. * true if WBRF is supported, otherwise returns false.
  163. */
  164. bool acpi_amd_wbrf_supported_consumer(struct device *dev)
  165. {
  166. struct acpi_device *adev;
  167. adev = ACPI_COMPANION(dev);
  168. if (!adev)
  169. return false;
  170. return acpi_check_dsm(adev->handle, &wifi_acpi_dsm_guid,
  171. WBRF_REVISION, BIT(WBRF_RETRIEVE));
  172. }
  173. EXPORT_SYMBOL_GPL(acpi_amd_wbrf_supported_consumer);
  174. /**
  175. * amd_wbrf_retrieve_freq_band - retrieve current active frequency bands
  176. *
  177. * @dev: device pointer
  178. * @out: output structure containing all the active frequency bands
  179. *
  180. * Retrieve the current active frequency bands which were broadcasted
  181. * by other producers. The consumer who calls this API should take
  182. * proper actions if any of the frequency band may cause RFI with its
  183. * own frequency band used.
  184. *
  185. * Return:
  186. * 0 for getting wifi freq band successfully.
  187. * Returns a negative error code for failure.
  188. */
  189. int amd_wbrf_retrieve_freq_band(struct device *dev, struct wbrf_ranges_in_out *out)
  190. {
  191. struct amd_wbrf_ranges_out acpi_out = {0};
  192. struct acpi_device *adev;
  193. union acpi_object *obj;
  194. union acpi_object param;
  195. int ret = 0;
  196. adev = ACPI_COMPANION(dev);
  197. if (!adev)
  198. return -ENODEV;
  199. param.type = ACPI_TYPE_STRING;
  200. param.string.length = 0;
  201. param.string.pointer = NULL;
  202. obj = acpi_evaluate_dsm(adev->handle, &wifi_acpi_dsm_guid,
  203. WBRF_REVISION, WBRF_RETRIEVE, &param);
  204. if (!obj)
  205. return -EINVAL;
  206. /*
  207. * The return buffer is with variable length and the format below:
  208. * number_of_entries(1 DWORD): Number of entries
  209. * start_freq of 1st entry(1 QWORD): Start frequency of the 1st entry
  210. * end_freq of 1st entry(1 QWORD): End frequency of the 1st entry
  211. * ...
  212. * ...
  213. * start_freq of the last entry(1 QWORD)
  214. * end_freq of the last entry(1 QWORD)
  215. *
  216. * Thus the buffer length is determined by the number of entries.
  217. * - For zero entry scenario, the buffer length will be 4 bytes.
  218. * - For one entry scenario, the buffer length will be 20 bytes.
  219. */
  220. if (obj->buffer.length > sizeof(acpi_out) || obj->buffer.length < 4) {
  221. dev_err(dev, "Wrong sized WBRT information");
  222. ret = -EINVAL;
  223. goto out;
  224. }
  225. memcpy(&acpi_out, obj->buffer.pointer, obj->buffer.length);
  226. out->num_of_ranges = acpi_out.num_of_ranges;
  227. memcpy(out->band_list, acpi_out.band_list, sizeof(acpi_out.band_list));
  228. out:
  229. ACPI_FREE(obj);
  230. return ret;
  231. }
  232. EXPORT_SYMBOL_GPL(amd_wbrf_retrieve_freq_band);
  233. /**
  234. * amd_wbrf_register_notifier - register for notifications of frequency
  235. * band update
  236. *
  237. * @nb: driver notifier block
  238. *
  239. * The consumer should register itself via this API so that it can get
  240. * notified on the frequency band updates from other producers.
  241. *
  242. * Return:
  243. * 0 for registering a consumer driver successfully.
  244. * Returns a negative error code for failure.
  245. */
  246. int amd_wbrf_register_notifier(struct notifier_block *nb)
  247. {
  248. return blocking_notifier_chain_register(&wbrf_chain_head, nb);
  249. }
  250. EXPORT_SYMBOL_GPL(amd_wbrf_register_notifier);
  251. /**
  252. * amd_wbrf_unregister_notifier - unregister for notifications of
  253. * frequency band update
  254. *
  255. * @nb: driver notifier block
  256. *
  257. * The consumer should call this API when it is longer interested with
  258. * the frequency band updates from other producers. Usually, this should
  259. * be performed during driver cleanup.
  260. *
  261. * Return:
  262. * 0 for unregistering a consumer driver.
  263. * Returns a negative error code for failure.
  264. */
  265. int amd_wbrf_unregister_notifier(struct notifier_block *nb)
  266. {
  267. return blocking_notifier_chain_unregister(&wbrf_chain_head, nb);
  268. }
  269. EXPORT_SYMBOL_GPL(amd_wbrf_unregister_notifier);