generic_bandwidth_allocation.c 9.6 KB

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