generic_bandwidth_allocation.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. // Copyright(c) 2015-2020 Intel Corporation.
  3. /*
  4. * Bandwidth management algorithm based on 2^n gears
  5. *
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/slab.h>
  12. #include <linux/soundwire/sdw.h>
  13. #include "bus.h"
  14. #define SDW_STRM_RATE_GROUPING 1
  15. struct sdw_group_params {
  16. unsigned int rate;
  17. int full_bw;
  18. int payload_bw;
  19. int hwidth;
  20. };
  21. struct sdw_group {
  22. unsigned int count;
  23. unsigned int max_size;
  24. unsigned int *rates;
  25. };
  26. void sdw_compute_slave_ports(struct sdw_master_runtime *m_rt,
  27. struct sdw_transport_data *t_data)
  28. {
  29. struct sdw_slave_runtime *s_rt = NULL;
  30. struct sdw_port_runtime *p_rt;
  31. int port_bo, sample_int;
  32. unsigned int rate, bps, ch = 0;
  33. unsigned int slave_total_ch;
  34. struct sdw_bus_params *b_params = &m_rt->bus->params;
  35. port_bo = t_data->block_offset;
  36. list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
  37. rate = m_rt->stream->params.rate;
  38. bps = m_rt->stream->params.bps;
  39. sample_int = (m_rt->bus->params.curr_dr_freq / rate);
  40. slave_total_ch = 0;
  41. list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
  42. ch = hweight32(p_rt->ch_mask);
  43. sdw_fill_xport_params(&p_rt->transport_params,
  44. p_rt->num, false,
  45. SDW_BLK_GRP_CNT_1,
  46. sample_int, port_bo, port_bo >> 8,
  47. t_data->hstart,
  48. t_data->hstop,
  49. SDW_BLK_PKG_PER_PORT, 0x0);
  50. sdw_fill_port_params(&p_rt->port_params,
  51. p_rt->num, bps,
  52. SDW_PORT_FLOW_MODE_ISOCH,
  53. b_params->s_data_mode);
  54. port_bo += bps * ch;
  55. slave_total_ch += ch;
  56. }
  57. if (m_rt->direction == SDW_DATA_DIR_TX &&
  58. m_rt->ch_count == slave_total_ch) {
  59. /*
  60. * Slave devices were configured to access all channels
  61. * of the stream, which indicates that they operate in
  62. * 'mirror mode'. Make sure we reset the port offset for
  63. * the next device in the list
  64. */
  65. port_bo = t_data->block_offset;
  66. }
  67. }
  68. }
  69. EXPORT_SYMBOL(sdw_compute_slave_ports);
  70. static void sdw_compute_master_ports(struct sdw_master_runtime *m_rt,
  71. struct sdw_group_params *params,
  72. int *port_bo, int hstop)
  73. {
  74. struct sdw_transport_data t_data = {0};
  75. struct sdw_port_runtime *p_rt;
  76. struct sdw_bus *bus = m_rt->bus;
  77. struct sdw_bus_params *b_params = &bus->params;
  78. int sample_int, hstart = 0;
  79. unsigned int rate, bps, ch;
  80. rate = m_rt->stream->params.rate;
  81. bps = m_rt->stream->params.bps;
  82. ch = m_rt->ch_count;
  83. sample_int = (bus->params.curr_dr_freq / rate);
  84. if (rate != params->rate)
  85. return;
  86. t_data.hstop = hstop;
  87. hstart = hstop - params->hwidth + 1;
  88. t_data.hstart = hstart;
  89. list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
  90. sdw_fill_xport_params(&p_rt->transport_params, p_rt->num,
  91. false, SDW_BLK_GRP_CNT_1, sample_int,
  92. *port_bo, (*port_bo) >> 8, hstart, hstop,
  93. SDW_BLK_PKG_PER_PORT, 0x0);
  94. sdw_fill_port_params(&p_rt->port_params,
  95. p_rt->num, bps,
  96. SDW_PORT_FLOW_MODE_ISOCH,
  97. b_params->m_data_mode);
  98. /* Check for first entry */
  99. if (!(p_rt == list_first_entry(&m_rt->port_list,
  100. struct sdw_port_runtime,
  101. port_node))) {
  102. (*port_bo) += bps * ch;
  103. continue;
  104. }
  105. t_data.hstart = hstart;
  106. t_data.hstop = hstop;
  107. t_data.block_offset = *port_bo;
  108. t_data.sub_block_offset = 0;
  109. (*port_bo) += bps * ch;
  110. }
  111. sdw_compute_slave_ports(m_rt, &t_data);
  112. }
  113. static void _sdw_compute_port_params(struct sdw_bus *bus,
  114. struct sdw_group_params *params, int count)
  115. {
  116. struct sdw_master_runtime *m_rt;
  117. int hstop = bus->params.col - 1;
  118. int port_bo, i;
  119. /* Run loop for all groups to compute transport parameters */
  120. for (i = 0; i < count; i++) {
  121. port_bo = 1;
  122. list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
  123. sdw_compute_master_ports(m_rt, &params[i], &port_bo, hstop);
  124. }
  125. hstop = hstop - params[i].hwidth;
  126. }
  127. }
  128. static int sdw_compute_group_params(struct sdw_bus *bus,
  129. struct sdw_group_params *params,
  130. int *rates, int count)
  131. {
  132. struct sdw_master_runtime *m_rt;
  133. int sel_col = bus->params.col;
  134. unsigned int rate, bps, ch;
  135. int i, column_needed = 0;
  136. /* Calculate bandwidth per group */
  137. for (i = 0; i < count; i++) {
  138. params[i].rate = rates[i];
  139. params[i].full_bw = bus->params.curr_dr_freq / params[i].rate;
  140. }
  141. list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
  142. rate = m_rt->stream->params.rate;
  143. bps = m_rt->stream->params.bps;
  144. ch = m_rt->ch_count;
  145. for (i = 0; i < count; i++) {
  146. if (rate == params[i].rate)
  147. params[i].payload_bw += bps * ch;
  148. }
  149. }
  150. for (i = 0; i < count; i++) {
  151. params[i].hwidth = (sel_col *
  152. params[i].payload_bw + params[i].full_bw - 1) /
  153. params[i].full_bw;
  154. column_needed += params[i].hwidth;
  155. }
  156. if (column_needed > sel_col - 1)
  157. return -EINVAL;
  158. return 0;
  159. }
  160. static int sdw_add_element_group_count(struct sdw_group *group,
  161. unsigned int rate)
  162. {
  163. int num = group->count;
  164. int i;
  165. for (i = 0; i <= num; i++) {
  166. if (rate == group->rates[i])
  167. break;
  168. if (i != num)
  169. continue;
  170. if (group->count >= group->max_size) {
  171. unsigned int *rates;
  172. group->max_size += 1;
  173. rates = krealloc(group->rates,
  174. (sizeof(int) * group->max_size),
  175. GFP_KERNEL);
  176. if (!rates)
  177. return -ENOMEM;
  178. group->rates = rates;
  179. }
  180. group->rates[group->count++] = rate;
  181. }
  182. return 0;
  183. }
  184. static int sdw_get_group_count(struct sdw_bus *bus,
  185. struct sdw_group *group)
  186. {
  187. struct sdw_master_runtime *m_rt;
  188. unsigned int rate;
  189. int ret = 0;
  190. group->count = 0;
  191. group->max_size = SDW_STRM_RATE_GROUPING;
  192. group->rates = kcalloc(group->max_size, sizeof(int), GFP_KERNEL);
  193. if (!group->rates)
  194. return -ENOMEM;
  195. list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
  196. rate = m_rt->stream->params.rate;
  197. if (m_rt == list_first_entry(&bus->m_rt_list,
  198. struct sdw_master_runtime,
  199. bus_node)) {
  200. group->rates[group->count++] = rate;
  201. } else {
  202. ret = sdw_add_element_group_count(group, rate);
  203. if (ret < 0) {
  204. kfree(group->rates);
  205. return ret;
  206. }
  207. }
  208. }
  209. return ret;
  210. }
  211. /**
  212. * sdw_compute_port_params: Compute transport and port parameters
  213. *
  214. * @bus: SDW Bus instance
  215. */
  216. static int sdw_compute_port_params(struct sdw_bus *bus)
  217. {
  218. struct sdw_group_params *params = NULL;
  219. struct sdw_group group;
  220. int ret;
  221. ret = sdw_get_group_count(bus, &group);
  222. if (ret < 0)
  223. return ret;
  224. if (group.count == 0)
  225. goto out;
  226. params = kcalloc(group.count, sizeof(*params), GFP_KERNEL);
  227. if (!params) {
  228. ret = -ENOMEM;
  229. goto out;
  230. }
  231. /* Compute transport parameters for grouped streams */
  232. ret = sdw_compute_group_params(bus, params,
  233. &group.rates[0], group.count);
  234. if (ret < 0)
  235. goto free_params;
  236. _sdw_compute_port_params(bus, params, group.count);
  237. free_params:
  238. kfree(params);
  239. out:
  240. kfree(group.rates);
  241. return ret;
  242. }
  243. static int sdw_select_row_col(struct sdw_bus *bus, int clk_freq)
  244. {
  245. struct sdw_master_prop *prop = &bus->prop;
  246. int frame_int, frame_freq;
  247. int r, c;
  248. for (c = 0; c < SDW_FRAME_COLS; c++) {
  249. for (r = 0; r < SDW_FRAME_ROWS; r++) {
  250. if (sdw_rows[r] != prop->default_row ||
  251. sdw_cols[c] != prop->default_col)
  252. continue;
  253. frame_int = sdw_rows[r] * sdw_cols[c];
  254. frame_freq = clk_freq / frame_int;
  255. if ((clk_freq - (frame_freq * SDW_FRAME_CTRL_BITS)) <
  256. bus->params.bandwidth)
  257. continue;
  258. bus->params.row = sdw_rows[r];
  259. bus->params.col = sdw_cols[c];
  260. return 0;
  261. }
  262. }
  263. return -EINVAL;
  264. }
  265. /**
  266. * sdw_compute_bus_params: Compute bus parameters
  267. *
  268. * @bus: SDW Bus instance
  269. */
  270. static int sdw_compute_bus_params(struct sdw_bus *bus)
  271. {
  272. unsigned int curr_dr_freq = 0;
  273. struct sdw_master_prop *mstr_prop = &bus->prop;
  274. int i, clk_values, ret;
  275. bool is_gear = false;
  276. u32 *clk_buf;
  277. if (mstr_prop->num_clk_gears) {
  278. clk_values = mstr_prop->num_clk_gears;
  279. clk_buf = mstr_prop->clk_gears;
  280. is_gear = true;
  281. } else if (mstr_prop->num_clk_freq) {
  282. clk_values = mstr_prop->num_clk_freq;
  283. clk_buf = mstr_prop->clk_freq;
  284. } else {
  285. clk_values = 1;
  286. clk_buf = NULL;
  287. }
  288. for (i = 0; i < clk_values; i++) {
  289. if (!clk_buf)
  290. curr_dr_freq = bus->params.max_dr_freq;
  291. else
  292. curr_dr_freq = (is_gear) ?
  293. (bus->params.max_dr_freq >> clk_buf[i]) :
  294. clk_buf[i] * SDW_DOUBLE_RATE_FACTOR;
  295. if (curr_dr_freq <= bus->params.bandwidth)
  296. continue;
  297. break;
  298. /*
  299. * TODO: Check all the Slave(s) port(s) audio modes and find
  300. * whether given clock rate is supported with glitchless
  301. * transition.
  302. */
  303. }
  304. if (i == clk_values) {
  305. dev_err(bus->dev, "%s: could not find clock value for bandwidth %d\n",
  306. __func__, bus->params.bandwidth);
  307. return -EINVAL;
  308. }
  309. ret = sdw_select_row_col(bus, curr_dr_freq);
  310. if (ret < 0) {
  311. dev_err(bus->dev, "%s: could not find frame configuration for bus dr_freq %d\n",
  312. __func__, curr_dr_freq);
  313. return -EINVAL;
  314. }
  315. bus->params.curr_dr_freq = curr_dr_freq;
  316. return 0;
  317. }
  318. /**
  319. * sdw_compute_params: Compute bus, transport and port parameters
  320. *
  321. * @bus: SDW Bus instance
  322. */
  323. int sdw_compute_params(struct sdw_bus *bus)
  324. {
  325. int ret;
  326. /* Computes clock frequency, frame shape and frame frequency */
  327. ret = sdw_compute_bus_params(bus);
  328. if (ret < 0)
  329. return ret;
  330. /* Compute transport and port params */
  331. ret = sdw_compute_port_params(bus);
  332. if (ret < 0) {
  333. dev_err(bus->dev, "Compute transport params failed: %d\n", ret);
  334. return ret;
  335. }
  336. return 0;
  337. }
  338. EXPORT_SYMBOL(sdw_compute_params);
  339. MODULE_LICENSE("Dual BSD/GPL");
  340. MODULE_DESCRIPTION("SoundWire Generic Bandwidth Allocation");