cppc_acpi.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. /*
  2. * CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers.
  3. *
  4. * (C) Copyright 2014, 2015 Linaro Ltd.
  5. * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. *
  12. * CPPC describes a few methods for controlling CPU performance using
  13. * information from a per CPU table called CPC. This table is described in
  14. * the ACPI v5.0+ specification. The table consists of a list of
  15. * registers which may be memory mapped or hardware registers and also may
  16. * include some static integer values.
  17. *
  18. * CPU performance is on an abstract continuous scale as against a discretized
  19. * P-state scale which is tied to CPU frequency only. In brief, the basic
  20. * operation involves:
  21. *
  22. * - OS makes a CPU performance request. (Can provide min and max bounds)
  23. *
  24. * - Platform (such as BMC) is free to optimize request within requested bounds
  25. * depending on power/thermal budgets etc.
  26. *
  27. * - Platform conveys its decision back to OS
  28. *
  29. * The communication between OS and platform occurs through another medium
  30. * called (PCC) Platform Communication Channel. This is a generic mailbox like
  31. * mechanism which includes doorbell semantics to indicate register updates.
  32. * See drivers/mailbox/pcc.c for details on PCC.
  33. *
  34. * Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and
  35. * above specifications.
  36. */
  37. #define pr_fmt(fmt) "ACPI CPPC: " fmt
  38. #include <linux/cpufreq.h>
  39. #include <linux/delay.h>
  40. #include <linux/iopoll.h>
  41. #include <linux/ktime.h>
  42. #include <linux/rwsem.h>
  43. #include <linux/wait.h>
  44. #include <acpi/cppc_acpi.h>
  45. struct cppc_pcc_data {
  46. struct mbox_chan *pcc_channel;
  47. void __iomem *pcc_comm_addr;
  48. bool pcc_channel_acquired;
  49. unsigned int deadline_us;
  50. unsigned int pcc_mpar, pcc_mrtt, pcc_nominal;
  51. bool pending_pcc_write_cmd; /* Any pending/batched PCC write cmds? */
  52. bool platform_owns_pcc; /* Ownership of PCC subspace */
  53. unsigned int pcc_write_cnt; /* Running count of PCC write commands */
  54. /*
  55. * Lock to provide controlled access to the PCC channel.
  56. *
  57. * For performance critical usecases(currently cppc_set_perf)
  58. * We need to take read_lock and check if channel belongs to OSPM
  59. * before reading or writing to PCC subspace
  60. * We need to take write_lock before transferring the channel
  61. * ownership to the platform via a Doorbell
  62. * This allows us to batch a number of CPPC requests if they happen
  63. * to originate in about the same time
  64. *
  65. * For non-performance critical usecases(init)
  66. * Take write_lock for all purposes which gives exclusive access
  67. */
  68. struct rw_semaphore pcc_lock;
  69. /* Wait queue for CPUs whose requests were batched */
  70. wait_queue_head_t pcc_write_wait_q;
  71. ktime_t last_cmd_cmpl_time;
  72. ktime_t last_mpar_reset;
  73. int mpar_count;
  74. int refcount;
  75. };
  76. /* Array to represent the PCC channel per subspace id */
  77. static struct cppc_pcc_data *pcc_data[MAX_PCC_SUBSPACES];
  78. /* The cpu_pcc_subspace_idx containsper CPU subspace id */
  79. static DEFINE_PER_CPU(int, cpu_pcc_subspace_idx);
  80. /*
  81. * The cpc_desc structure contains the ACPI register details
  82. * as described in the per CPU _CPC tables. The details
  83. * include the type of register (e.g. PCC, System IO, FFH etc.)
  84. * and destination addresses which lets us READ/WRITE CPU performance
  85. * information using the appropriate I/O methods.
  86. */
  87. static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr);
  88. /* pcc mapped address + header size + offset within PCC subspace */
  89. #define GET_PCC_VADDR(offs, pcc_ss_id) (pcc_data[pcc_ss_id]->pcc_comm_addr + \
  90. 0x8 + (offs))
  91. /* Check if a CPC register is in PCC */
  92. #define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
  93. (cpc)->cpc_entry.reg.space_id == \
  94. ACPI_ADR_SPACE_PLATFORM_COMM)
  95. /* Evalutes to True if reg is a NULL register descriptor */
  96. #define IS_NULL_REG(reg) ((reg)->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY && \
  97. (reg)->address == 0 && \
  98. (reg)->bit_width == 0 && \
  99. (reg)->bit_offset == 0 && \
  100. (reg)->access_width == 0)
  101. /* Evalutes to True if an optional cpc field is supported */
  102. #define CPC_SUPPORTED(cpc) ((cpc)->type == ACPI_TYPE_INTEGER ? \
  103. !!(cpc)->cpc_entry.int_value : \
  104. !IS_NULL_REG(&(cpc)->cpc_entry.reg))
  105. /*
  106. * Arbitrary Retries in case the remote processor is slow to respond
  107. * to PCC commands. Keeping it high enough to cover emulators where
  108. * the processors run painfully slow.
  109. */
  110. #define NUM_RETRIES 500ULL
  111. struct cppc_attr {
  112. struct attribute attr;
  113. ssize_t (*show)(struct kobject *kobj,
  114. struct attribute *attr, char *buf);
  115. ssize_t (*store)(struct kobject *kobj,
  116. struct attribute *attr, const char *c, ssize_t count);
  117. };
  118. #define define_one_cppc_ro(_name) \
  119. static struct cppc_attr _name = \
  120. __ATTR(_name, 0444, show_##_name, NULL)
  121. #define to_cpc_desc(a) container_of(a, struct cpc_desc, kobj)
  122. #define show_cppc_data(access_fn, struct_name, member_name) \
  123. static ssize_t show_##member_name(struct kobject *kobj, \
  124. struct attribute *attr, char *buf) \
  125. { \
  126. struct cpc_desc *cpc_ptr = to_cpc_desc(kobj); \
  127. struct struct_name st_name = {0}; \
  128. int ret; \
  129. \
  130. ret = access_fn(cpc_ptr->cpu_id, &st_name); \
  131. if (ret) \
  132. return ret; \
  133. \
  134. return scnprintf(buf, PAGE_SIZE, "%llu\n", \
  135. (u64)st_name.member_name); \
  136. } \
  137. define_one_cppc_ro(member_name)
  138. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, highest_perf);
  139. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_perf);
  140. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_perf);
  141. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_nonlinear_perf);
  142. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_freq);
  143. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_freq);
  144. show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, reference_perf);
  145. show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, wraparound_time);
  146. static ssize_t show_feedback_ctrs(struct kobject *kobj,
  147. struct attribute *attr, char *buf)
  148. {
  149. struct cpc_desc *cpc_ptr = to_cpc_desc(kobj);
  150. struct cppc_perf_fb_ctrs fb_ctrs = {0};
  151. int ret;
  152. ret = cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs);
  153. if (ret)
  154. return ret;
  155. return scnprintf(buf, PAGE_SIZE, "ref:%llu del:%llu\n",
  156. fb_ctrs.reference, fb_ctrs.delivered);
  157. }
  158. define_one_cppc_ro(feedback_ctrs);
  159. static struct attribute *cppc_attrs[] = {
  160. &feedback_ctrs.attr,
  161. &reference_perf.attr,
  162. &wraparound_time.attr,
  163. &highest_perf.attr,
  164. &lowest_perf.attr,
  165. &lowest_nonlinear_perf.attr,
  166. &nominal_perf.attr,
  167. &nominal_freq.attr,
  168. &lowest_freq.attr,
  169. NULL
  170. };
  171. static struct kobj_type cppc_ktype = {
  172. .sysfs_ops = &kobj_sysfs_ops,
  173. .default_attrs = cppc_attrs,
  174. };
  175. static int check_pcc_chan(int pcc_ss_id, bool chk_err_bit)
  176. {
  177. int ret, status;
  178. struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
  179. struct acpi_pcct_shared_memory __iomem *generic_comm_base =
  180. pcc_ss_data->pcc_comm_addr;
  181. if (!pcc_ss_data->platform_owns_pcc)
  182. return 0;
  183. /*
  184. * Poll PCC status register every 3us(delay_us) for maximum of
  185. * deadline_us(timeout_us) until PCC command complete bit is set(cond)
  186. */
  187. ret = readw_relaxed_poll_timeout(&generic_comm_base->status, status,
  188. status & PCC_CMD_COMPLETE_MASK, 3,
  189. pcc_ss_data->deadline_us);
  190. if (likely(!ret)) {
  191. pcc_ss_data->platform_owns_pcc = false;
  192. if (chk_err_bit && (status & PCC_ERROR_MASK))
  193. ret = -EIO;
  194. }
  195. if (unlikely(ret))
  196. pr_err("PCC check channel failed for ss: %d. ret=%d\n",
  197. pcc_ss_id, ret);
  198. return ret;
  199. }
  200. /*
  201. * This function transfers the ownership of the PCC to the platform
  202. * So it must be called while holding write_lock(pcc_lock)
  203. */
  204. static int send_pcc_cmd(int pcc_ss_id, u16 cmd)
  205. {
  206. int ret = -EIO, i;
  207. struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
  208. struct acpi_pcct_shared_memory *generic_comm_base =
  209. (struct acpi_pcct_shared_memory *)pcc_ss_data->pcc_comm_addr;
  210. unsigned int time_delta;
  211. /*
  212. * For CMD_WRITE we know for a fact the caller should have checked
  213. * the channel before writing to PCC space
  214. */
  215. if (cmd == CMD_READ) {
  216. /*
  217. * If there are pending cpc_writes, then we stole the channel
  218. * before write completion, so first send a WRITE command to
  219. * platform
  220. */
  221. if (pcc_ss_data->pending_pcc_write_cmd)
  222. send_pcc_cmd(pcc_ss_id, CMD_WRITE);
  223. ret = check_pcc_chan(pcc_ss_id, false);
  224. if (ret)
  225. goto end;
  226. } else /* CMD_WRITE */
  227. pcc_ss_data->pending_pcc_write_cmd = FALSE;
  228. /*
  229. * Handle the Minimum Request Turnaround Time(MRTT)
  230. * "The minimum amount of time that OSPM must wait after the completion
  231. * of a command before issuing the next command, in microseconds"
  232. */
  233. if (pcc_ss_data->pcc_mrtt) {
  234. time_delta = ktime_us_delta(ktime_get(),
  235. pcc_ss_data->last_cmd_cmpl_time);
  236. if (pcc_ss_data->pcc_mrtt > time_delta)
  237. udelay(pcc_ss_data->pcc_mrtt - time_delta);
  238. }
  239. /*
  240. * Handle the non-zero Maximum Periodic Access Rate(MPAR)
  241. * "The maximum number of periodic requests that the subspace channel can
  242. * support, reported in commands per minute. 0 indicates no limitation."
  243. *
  244. * This parameter should be ideally zero or large enough so that it can
  245. * handle maximum number of requests that all the cores in the system can
  246. * collectively generate. If it is not, we will follow the spec and just
  247. * not send the request to the platform after hitting the MPAR limit in
  248. * any 60s window
  249. */
  250. if (pcc_ss_data->pcc_mpar) {
  251. if (pcc_ss_data->mpar_count == 0) {
  252. time_delta = ktime_ms_delta(ktime_get(),
  253. pcc_ss_data->last_mpar_reset);
  254. if ((time_delta < 60 * MSEC_PER_SEC) && pcc_ss_data->last_mpar_reset) {
  255. pr_debug("PCC cmd for subspace %d not sent due to MPAR limit",
  256. pcc_ss_id);
  257. ret = -EIO;
  258. goto end;
  259. }
  260. pcc_ss_data->last_mpar_reset = ktime_get();
  261. pcc_ss_data->mpar_count = pcc_ss_data->pcc_mpar;
  262. }
  263. pcc_ss_data->mpar_count--;
  264. }
  265. /* Write to the shared comm region. */
  266. writew_relaxed(cmd, &generic_comm_base->command);
  267. /* Flip CMD COMPLETE bit */
  268. writew_relaxed(0, &generic_comm_base->status);
  269. pcc_ss_data->platform_owns_pcc = true;
  270. /* Ring doorbell */
  271. ret = mbox_send_message(pcc_ss_data->pcc_channel, &cmd);
  272. if (ret < 0) {
  273. pr_err("Err sending PCC mbox message. ss: %d cmd:%d, ret:%d\n",
  274. pcc_ss_id, cmd, ret);
  275. goto end;
  276. }
  277. /* wait for completion and check for PCC errro bit */
  278. ret = check_pcc_chan(pcc_ss_id, true);
  279. if (pcc_ss_data->pcc_mrtt)
  280. pcc_ss_data->last_cmd_cmpl_time = ktime_get();
  281. if (pcc_ss_data->pcc_channel->mbox->txdone_irq)
  282. mbox_chan_txdone(pcc_ss_data->pcc_channel, ret);
  283. else
  284. mbox_client_txdone(pcc_ss_data->pcc_channel, ret);
  285. end:
  286. if (cmd == CMD_WRITE) {
  287. if (unlikely(ret)) {
  288. for_each_possible_cpu(i) {
  289. struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i);
  290. if (!desc)
  291. continue;
  292. if (desc->write_cmd_id == pcc_ss_data->pcc_write_cnt)
  293. desc->write_cmd_status = ret;
  294. }
  295. }
  296. pcc_ss_data->pcc_write_cnt++;
  297. wake_up_all(&pcc_ss_data->pcc_write_wait_q);
  298. }
  299. return ret;
  300. }
  301. static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
  302. {
  303. if (ret < 0)
  304. pr_debug("TX did not complete: CMD sent:%x, ret:%d\n",
  305. *(u16 *)msg, ret);
  306. else
  307. pr_debug("TX completed. CMD sent:%x, ret:%d\n",
  308. *(u16 *)msg, ret);
  309. }
  310. struct mbox_client cppc_mbox_cl = {
  311. .tx_done = cppc_chan_tx_done,
  312. .knows_txdone = true,
  313. };
  314. static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle)
  315. {
  316. int result = -EFAULT;
  317. acpi_status status = AE_OK;
  318. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  319. struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
  320. struct acpi_buffer state = {0, NULL};
  321. union acpi_object *psd = NULL;
  322. struct acpi_psd_package *pdomain;
  323. status = acpi_evaluate_object_typed(handle, "_PSD", NULL,
  324. &buffer, ACPI_TYPE_PACKAGE);
  325. if (status == AE_NOT_FOUND) /* _PSD is optional */
  326. return 0;
  327. if (ACPI_FAILURE(status))
  328. return -ENODEV;
  329. psd = buffer.pointer;
  330. if (!psd || psd->package.count != 1) {
  331. pr_debug("Invalid _PSD data\n");
  332. goto end;
  333. }
  334. pdomain = &(cpc_ptr->domain_info);
  335. state.length = sizeof(struct acpi_psd_package);
  336. state.pointer = pdomain;
  337. status = acpi_extract_package(&(psd->package.elements[0]),
  338. &format, &state);
  339. if (ACPI_FAILURE(status)) {
  340. pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id);
  341. goto end;
  342. }
  343. if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
  344. pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id);
  345. goto end;
  346. }
  347. if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
  348. pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id);
  349. goto end;
  350. }
  351. if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
  352. pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
  353. pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
  354. pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id);
  355. goto end;
  356. }
  357. result = 0;
  358. end:
  359. kfree(buffer.pointer);
  360. return result;
  361. }
  362. /**
  363. * acpi_get_psd_map - Map the CPUs in a common freq domain.
  364. * @all_cpu_data: Ptrs to CPU specific CPPC data including PSD info.
  365. *
  366. * Return: 0 for success or negative value for err.
  367. */
  368. int acpi_get_psd_map(struct cppc_cpudata **all_cpu_data)
  369. {
  370. int count_target;
  371. int retval = 0;
  372. unsigned int i, j;
  373. cpumask_var_t covered_cpus;
  374. struct cppc_cpudata *pr, *match_pr;
  375. struct acpi_psd_package *pdomain;
  376. struct acpi_psd_package *match_pdomain;
  377. struct cpc_desc *cpc_ptr, *match_cpc_ptr;
  378. if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
  379. return -ENOMEM;
  380. /*
  381. * Now that we have _PSD data from all CPUs, lets setup P-state
  382. * domain info.
  383. */
  384. for_each_possible_cpu(i) {
  385. pr = all_cpu_data[i];
  386. if (!pr)
  387. continue;
  388. if (cpumask_test_cpu(i, covered_cpus))
  389. continue;
  390. cpc_ptr = per_cpu(cpc_desc_ptr, i);
  391. if (!cpc_ptr) {
  392. retval = -EFAULT;
  393. goto err_ret;
  394. }
  395. pdomain = &(cpc_ptr->domain_info);
  396. cpumask_set_cpu(i, pr->shared_cpu_map);
  397. cpumask_set_cpu(i, covered_cpus);
  398. if (pdomain->num_processors <= 1)
  399. continue;
  400. /* Validate the Domain info */
  401. count_target = pdomain->num_processors;
  402. if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
  403. pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  404. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
  405. pr->shared_type = CPUFREQ_SHARED_TYPE_HW;
  406. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
  407. pr->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  408. for_each_possible_cpu(j) {
  409. if (i == j)
  410. continue;
  411. match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
  412. if (!match_cpc_ptr) {
  413. retval = -EFAULT;
  414. goto err_ret;
  415. }
  416. match_pdomain = &(match_cpc_ptr->domain_info);
  417. if (match_pdomain->domain != pdomain->domain)
  418. continue;
  419. /* Here i and j are in the same domain */
  420. if (match_pdomain->num_processors != count_target) {
  421. retval = -EFAULT;
  422. goto err_ret;
  423. }
  424. if (pdomain->coord_type != match_pdomain->coord_type) {
  425. retval = -EFAULT;
  426. goto err_ret;
  427. }
  428. cpumask_set_cpu(j, covered_cpus);
  429. cpumask_set_cpu(j, pr->shared_cpu_map);
  430. }
  431. for_each_possible_cpu(j) {
  432. if (i == j)
  433. continue;
  434. match_pr = all_cpu_data[j];
  435. if (!match_pr)
  436. continue;
  437. match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
  438. if (!match_cpc_ptr) {
  439. retval = -EFAULT;
  440. goto err_ret;
  441. }
  442. match_pdomain = &(match_cpc_ptr->domain_info);
  443. if (match_pdomain->domain != pdomain->domain)
  444. continue;
  445. match_pr->shared_type = pr->shared_type;
  446. cpumask_copy(match_pr->shared_cpu_map,
  447. pr->shared_cpu_map);
  448. }
  449. }
  450. err_ret:
  451. for_each_possible_cpu(i) {
  452. pr = all_cpu_data[i];
  453. if (!pr)
  454. continue;
  455. /* Assume no coordination on any error parsing domain info */
  456. if (retval) {
  457. cpumask_clear(pr->shared_cpu_map);
  458. cpumask_set_cpu(i, pr->shared_cpu_map);
  459. pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  460. }
  461. }
  462. free_cpumask_var(covered_cpus);
  463. return retval;
  464. }
  465. EXPORT_SYMBOL_GPL(acpi_get_psd_map);
  466. static int register_pcc_channel(int pcc_ss_idx)
  467. {
  468. struct acpi_pcct_hw_reduced *cppc_ss;
  469. u64 usecs_lat;
  470. if (pcc_ss_idx >= 0) {
  471. pcc_data[pcc_ss_idx]->pcc_channel =
  472. pcc_mbox_request_channel(&cppc_mbox_cl, pcc_ss_idx);
  473. if (IS_ERR(pcc_data[pcc_ss_idx]->pcc_channel)) {
  474. pr_err("Failed to find PCC channel for subspace %d\n",
  475. pcc_ss_idx);
  476. return -ENODEV;
  477. }
  478. /*
  479. * The PCC mailbox controller driver should
  480. * have parsed the PCCT (global table of all
  481. * PCC channels) and stored pointers to the
  482. * subspace communication region in con_priv.
  483. */
  484. cppc_ss = (pcc_data[pcc_ss_idx]->pcc_channel)->con_priv;
  485. if (!cppc_ss) {
  486. pr_err("No PCC subspace found for %d CPPC\n",
  487. pcc_ss_idx);
  488. return -ENODEV;
  489. }
  490. /*
  491. * cppc_ss->latency is just a Nominal value. In reality
  492. * the remote processor could be much slower to reply.
  493. * So add an arbitrary amount of wait on top of Nominal.
  494. */
  495. usecs_lat = NUM_RETRIES * cppc_ss->latency;
  496. pcc_data[pcc_ss_idx]->deadline_us = usecs_lat;
  497. pcc_data[pcc_ss_idx]->pcc_mrtt = cppc_ss->min_turnaround_time;
  498. pcc_data[pcc_ss_idx]->pcc_mpar = cppc_ss->max_access_rate;
  499. pcc_data[pcc_ss_idx]->pcc_nominal = cppc_ss->latency;
  500. pcc_data[pcc_ss_idx]->pcc_comm_addr =
  501. acpi_os_ioremap(cppc_ss->base_address, cppc_ss->length);
  502. if (!pcc_data[pcc_ss_idx]->pcc_comm_addr) {
  503. pr_err("Failed to ioremap PCC comm region mem for %d\n",
  504. pcc_ss_idx);
  505. return -ENOMEM;
  506. }
  507. /* Set flag so that we dont come here for each CPU. */
  508. pcc_data[pcc_ss_idx]->pcc_channel_acquired = true;
  509. }
  510. return 0;
  511. }
  512. /**
  513. * cpc_ffh_supported() - check if FFH reading supported
  514. *
  515. * Check if the architecture has support for functional fixed hardware
  516. * read/write capability.
  517. *
  518. * Return: true for supported, false for not supported
  519. */
  520. bool __weak cpc_ffh_supported(void)
  521. {
  522. return false;
  523. }
  524. /**
  525. * pcc_data_alloc() - Allocate the pcc_data memory for pcc subspace
  526. *
  527. * Check and allocate the cppc_pcc_data memory.
  528. * In some processor configurations it is possible that same subspace
  529. * is shared between multiple CPU's. This is seen especially in CPU's
  530. * with hardware multi-threading support.
  531. *
  532. * Return: 0 for success, errno for failure
  533. */
  534. int pcc_data_alloc(int pcc_ss_id)
  535. {
  536. if (pcc_ss_id < 0 || pcc_ss_id >= MAX_PCC_SUBSPACES)
  537. return -EINVAL;
  538. if (pcc_data[pcc_ss_id]) {
  539. pcc_data[pcc_ss_id]->refcount++;
  540. } else {
  541. pcc_data[pcc_ss_id] = kzalloc(sizeof(struct cppc_pcc_data),
  542. GFP_KERNEL);
  543. if (!pcc_data[pcc_ss_id])
  544. return -ENOMEM;
  545. pcc_data[pcc_ss_id]->refcount++;
  546. }
  547. return 0;
  548. }
  549. /* Check if CPPC revision + num_ent combination is supported */
  550. static bool is_cppc_supported(int revision, int num_ent)
  551. {
  552. int expected_num_ent;
  553. switch (revision) {
  554. case CPPC_V2_REV:
  555. expected_num_ent = CPPC_V2_NUM_ENT;
  556. break;
  557. case CPPC_V3_REV:
  558. expected_num_ent = CPPC_V3_NUM_ENT;
  559. break;
  560. default:
  561. pr_debug("Firmware exports unsupported CPPC revision: %d\n",
  562. revision);
  563. return false;
  564. }
  565. if (expected_num_ent != num_ent) {
  566. pr_debug("Firmware exports %d entries. Expected: %d for CPPC rev:%d\n",
  567. num_ent, expected_num_ent, revision);
  568. return false;
  569. }
  570. return true;
  571. }
  572. /*
  573. * An example CPC table looks like the following.
  574. *
  575. * Name(_CPC, Package()
  576. * {
  577. * 17,
  578. * NumEntries
  579. * 1,
  580. * // Revision
  581. * ResourceTemplate(){Register(PCC, 32, 0, 0x120, 2)},
  582. * // Highest Performance
  583. * ResourceTemplate(){Register(PCC, 32, 0, 0x124, 2)},
  584. * // Nominal Performance
  585. * ResourceTemplate(){Register(PCC, 32, 0, 0x128, 2)},
  586. * // Lowest Nonlinear Performance
  587. * ResourceTemplate(){Register(PCC, 32, 0, 0x12C, 2)},
  588. * // Lowest Performance
  589. * ResourceTemplate(){Register(PCC, 32, 0, 0x130, 2)},
  590. * // Guaranteed Performance Register
  591. * ResourceTemplate(){Register(PCC, 32, 0, 0x110, 2)},
  592. * // Desired Performance Register
  593. * ResourceTemplate(){Register(SystemMemory, 0, 0, 0, 0)},
  594. * ..
  595. * ..
  596. * ..
  597. *
  598. * }
  599. * Each Register() encodes how to access that specific register.
  600. * e.g. a sample PCC entry has the following encoding:
  601. *
  602. * Register (
  603. * PCC,
  604. * AddressSpaceKeyword
  605. * 8,
  606. * //RegisterBitWidth
  607. * 8,
  608. * //RegisterBitOffset
  609. * 0x30,
  610. * //RegisterAddress
  611. * 9
  612. * //AccessSize (subspace ID)
  613. * 0
  614. * )
  615. * }
  616. */
  617. /**
  618. * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
  619. * @pr: Ptr to acpi_processor containing this CPUs logical Id.
  620. *
  621. * Return: 0 for success or negative value for err.
  622. */
  623. int acpi_cppc_processor_probe(struct acpi_processor *pr)
  624. {
  625. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  626. union acpi_object *out_obj, *cpc_obj;
  627. struct cpc_desc *cpc_ptr;
  628. struct cpc_reg *gas_t;
  629. struct device *cpu_dev;
  630. acpi_handle handle = pr->handle;
  631. unsigned int num_ent, i, cpc_rev;
  632. int pcc_subspace_id = -1;
  633. acpi_status status;
  634. int ret = -EFAULT;
  635. /* Parse the ACPI _CPC table for this cpu. */
  636. status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
  637. ACPI_TYPE_PACKAGE);
  638. if (ACPI_FAILURE(status)) {
  639. ret = -ENODEV;
  640. goto out_buf_free;
  641. }
  642. out_obj = (union acpi_object *) output.pointer;
  643. cpc_ptr = kzalloc(sizeof(struct cpc_desc), GFP_KERNEL);
  644. if (!cpc_ptr) {
  645. ret = -ENOMEM;
  646. goto out_buf_free;
  647. }
  648. /* First entry is NumEntries. */
  649. cpc_obj = &out_obj->package.elements[0];
  650. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  651. num_ent = cpc_obj->integer.value;
  652. } else {
  653. pr_debug("Unexpected entry type(%d) for NumEntries\n",
  654. cpc_obj->type);
  655. goto out_free;
  656. }
  657. cpc_ptr->num_entries = num_ent;
  658. /* Second entry should be revision. */
  659. cpc_obj = &out_obj->package.elements[1];
  660. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  661. cpc_rev = cpc_obj->integer.value;
  662. } else {
  663. pr_debug("Unexpected entry type(%d) for Revision\n",
  664. cpc_obj->type);
  665. goto out_free;
  666. }
  667. cpc_ptr->version = cpc_rev;
  668. if (!is_cppc_supported(cpc_rev, num_ent))
  669. goto out_free;
  670. /* Iterate through remaining entries in _CPC */
  671. for (i = 2; i < num_ent; i++) {
  672. cpc_obj = &out_obj->package.elements[i];
  673. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  674. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
  675. cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value;
  676. } else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
  677. gas_t = (struct cpc_reg *)
  678. cpc_obj->buffer.pointer;
  679. /*
  680. * The PCC Subspace index is encoded inside
  681. * the CPC table entries. The same PCC index
  682. * will be used for all the PCC entries,
  683. * so extract it only once.
  684. */
  685. if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  686. if (pcc_subspace_id < 0) {
  687. pcc_subspace_id = gas_t->access_width;
  688. if (pcc_data_alloc(pcc_subspace_id))
  689. goto out_free;
  690. } else if (pcc_subspace_id != gas_t->access_width) {
  691. pr_debug("Mismatched PCC ids.\n");
  692. goto out_free;
  693. }
  694. } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  695. if (gas_t->address) {
  696. void __iomem *addr;
  697. addr = ioremap(gas_t->address, gas_t->bit_width/8);
  698. if (!addr)
  699. goto out_free;
  700. cpc_ptr->cpc_regs[i-2].sys_mem_vaddr = addr;
  701. }
  702. } else {
  703. if (gas_t->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported()) {
  704. /* Support only PCC ,SYS MEM and FFH type regs */
  705. pr_debug("Unsupported register type: %d\n", gas_t->space_id);
  706. goto out_free;
  707. }
  708. }
  709. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER;
  710. memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t));
  711. } else {
  712. pr_debug("Err in entry:%d in CPC table of CPU:%d \n", i, pr->id);
  713. goto out_free;
  714. }
  715. }
  716. per_cpu(cpu_pcc_subspace_idx, pr->id) = pcc_subspace_id;
  717. /*
  718. * Initialize the remaining cpc_regs as unsupported.
  719. * Example: In case FW exposes CPPC v2, the below loop will initialize
  720. * LOWEST_FREQ and NOMINAL_FREQ regs as unsupported
  721. */
  722. for (i = num_ent - 2; i < MAX_CPC_REG_ENT; i++) {
  723. cpc_ptr->cpc_regs[i].type = ACPI_TYPE_INTEGER;
  724. cpc_ptr->cpc_regs[i].cpc_entry.int_value = 0;
  725. }
  726. /* Store CPU Logical ID */
  727. cpc_ptr->cpu_id = pr->id;
  728. /* Parse PSD data for this CPU */
  729. ret = acpi_get_psd(cpc_ptr, handle);
  730. if (ret)
  731. goto out_free;
  732. /* Register PCC channel once for all PCC subspace id. */
  733. if (pcc_subspace_id >= 0 && !pcc_data[pcc_subspace_id]->pcc_channel_acquired) {
  734. ret = register_pcc_channel(pcc_subspace_id);
  735. if (ret)
  736. goto out_free;
  737. init_rwsem(&pcc_data[pcc_subspace_id]->pcc_lock);
  738. init_waitqueue_head(&pcc_data[pcc_subspace_id]->pcc_write_wait_q);
  739. }
  740. /* Everything looks okay */
  741. pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
  742. /* Add per logical CPU nodes for reading its feedback counters. */
  743. cpu_dev = get_cpu_device(pr->id);
  744. if (!cpu_dev) {
  745. ret = -EINVAL;
  746. goto out_free;
  747. }
  748. /* Plug PSD data into this CPUs CPC descriptor. */
  749. per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr;
  750. ret = kobject_init_and_add(&cpc_ptr->kobj, &cppc_ktype, &cpu_dev->kobj,
  751. "acpi_cppc");
  752. if (ret) {
  753. per_cpu(cpc_desc_ptr, pr->id) = NULL;
  754. goto out_free;
  755. }
  756. kfree(output.pointer);
  757. return 0;
  758. out_free:
  759. /* Free all the mapped sys mem areas for this CPU */
  760. for (i = 2; i < cpc_ptr->num_entries; i++) {
  761. void __iomem *addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
  762. if (addr)
  763. iounmap(addr);
  764. }
  765. kfree(cpc_ptr);
  766. out_buf_free:
  767. kfree(output.pointer);
  768. return ret;
  769. }
  770. EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
  771. /**
  772. * acpi_cppc_processor_exit - Cleanup CPC structs.
  773. * @pr: Ptr to acpi_processor containing this CPUs logical Id.
  774. *
  775. * Return: Void
  776. */
  777. void acpi_cppc_processor_exit(struct acpi_processor *pr)
  778. {
  779. struct cpc_desc *cpc_ptr;
  780. unsigned int i;
  781. void __iomem *addr;
  782. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, pr->id);
  783. if (pcc_ss_id >=0 && pcc_data[pcc_ss_id]) {
  784. if (pcc_data[pcc_ss_id]->pcc_channel_acquired) {
  785. pcc_data[pcc_ss_id]->refcount--;
  786. if (!pcc_data[pcc_ss_id]->refcount) {
  787. pcc_mbox_free_channel(pcc_data[pcc_ss_id]->pcc_channel);
  788. pcc_data[pcc_ss_id]->pcc_channel_acquired = 0;
  789. kfree(pcc_data[pcc_ss_id]);
  790. }
  791. }
  792. }
  793. cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
  794. if (!cpc_ptr)
  795. return;
  796. /* Free all the mapped sys mem areas for this CPU */
  797. for (i = 2; i < cpc_ptr->num_entries; i++) {
  798. addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
  799. if (addr)
  800. iounmap(addr);
  801. }
  802. kobject_put(&cpc_ptr->kobj);
  803. kfree(cpc_ptr);
  804. }
  805. EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
  806. /**
  807. * cpc_read_ffh() - Read FFH register
  808. * @cpunum: cpu number to read
  809. * @reg: cppc register information
  810. * @val: place holder for return value
  811. *
  812. * Read bit_width bits from a specified address and bit_offset
  813. *
  814. * Return: 0 for success and error code
  815. */
  816. int __weak cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
  817. {
  818. return -ENOTSUPP;
  819. }
  820. /**
  821. * cpc_write_ffh() - Write FFH register
  822. * @cpunum: cpu number to write
  823. * @reg: cppc register information
  824. * @val: value to write
  825. *
  826. * Write value of bit_width bits to a specified address and bit_offset
  827. *
  828. * Return: 0 for success and error code
  829. */
  830. int __weak cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
  831. {
  832. return -ENOTSUPP;
  833. }
  834. /*
  835. * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
  836. * as fast as possible. We have already mapped the PCC subspace during init, so
  837. * we can directly write to it.
  838. */
  839. static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val)
  840. {
  841. int ret_val = 0;
  842. void __iomem *vaddr = 0;
  843. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  844. struct cpc_reg *reg = &reg_res->cpc_entry.reg;
  845. if (reg_res->type == ACPI_TYPE_INTEGER) {
  846. *val = reg_res->cpc_entry.int_value;
  847. return ret_val;
  848. }
  849. *val = 0;
  850. if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0)
  851. vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
  852. else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  853. vaddr = reg_res->sys_mem_vaddr;
  854. else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
  855. return cpc_read_ffh(cpu, reg, val);
  856. else
  857. return acpi_os_read_memory((acpi_physical_address)reg->address,
  858. val, reg->bit_width);
  859. switch (reg->bit_width) {
  860. case 8:
  861. *val = readb_relaxed(vaddr);
  862. break;
  863. case 16:
  864. *val = readw_relaxed(vaddr);
  865. break;
  866. case 32:
  867. *val = readl_relaxed(vaddr);
  868. break;
  869. case 64:
  870. *val = readq_relaxed(vaddr);
  871. break;
  872. default:
  873. pr_debug("Error: Cannot read %u bit width from PCC for ss: %d\n",
  874. reg->bit_width, pcc_ss_id);
  875. ret_val = -EFAULT;
  876. }
  877. return ret_val;
  878. }
  879. static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)
  880. {
  881. int ret_val = 0;
  882. void __iomem *vaddr = 0;
  883. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  884. struct cpc_reg *reg = &reg_res->cpc_entry.reg;
  885. if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0)
  886. vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
  887. else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  888. vaddr = reg_res->sys_mem_vaddr;
  889. else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
  890. return cpc_write_ffh(cpu, reg, val);
  891. else
  892. return acpi_os_write_memory((acpi_physical_address)reg->address,
  893. val, reg->bit_width);
  894. switch (reg->bit_width) {
  895. case 8:
  896. writeb_relaxed(val, vaddr);
  897. break;
  898. case 16:
  899. writew_relaxed(val, vaddr);
  900. break;
  901. case 32:
  902. writel_relaxed(val, vaddr);
  903. break;
  904. case 64:
  905. writeq_relaxed(val, vaddr);
  906. break;
  907. default:
  908. pr_debug("Error: Cannot write %u bit width to PCC for ss: %d\n",
  909. reg->bit_width, pcc_ss_id);
  910. ret_val = -EFAULT;
  911. break;
  912. }
  913. return ret_val;
  914. }
  915. /**
  916. * cppc_get_perf_caps - Get a CPUs performance capabilities.
  917. * @cpunum: CPU from which to get capabilities info.
  918. * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
  919. *
  920. * Return: 0 for success with perf_caps populated else -ERRNO.
  921. */
  922. int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
  923. {
  924. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  925. struct cpc_register_resource *highest_reg, *lowest_reg,
  926. *lowest_non_linear_reg, *nominal_reg,
  927. *low_freq_reg = NULL, *nom_freq_reg = NULL;
  928. u64 high, low, nom, min_nonlinear, low_f = 0, nom_f = 0;
  929. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
  930. struct cppc_pcc_data *pcc_ss_data = NULL;
  931. int ret = 0, regs_in_pcc = 0;
  932. if (!cpc_desc) {
  933. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  934. return -ENODEV;
  935. }
  936. highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
  937. lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
  938. lowest_non_linear_reg = &cpc_desc->cpc_regs[LOW_NON_LINEAR_PERF];
  939. nominal_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
  940. low_freq_reg = &cpc_desc->cpc_regs[LOWEST_FREQ];
  941. nom_freq_reg = &cpc_desc->cpc_regs[NOMINAL_FREQ];
  942. /* Are any of the regs PCC ?*/
  943. if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) ||
  944. CPC_IN_PCC(lowest_non_linear_reg) || CPC_IN_PCC(nominal_reg) ||
  945. CPC_IN_PCC(low_freq_reg) || CPC_IN_PCC(nom_freq_reg)) {
  946. if (pcc_ss_id < 0) {
  947. pr_debug("Invalid pcc_ss_id\n");
  948. return -ENODEV;
  949. }
  950. pcc_ss_data = pcc_data[pcc_ss_id];
  951. regs_in_pcc = 1;
  952. down_write(&pcc_ss_data->pcc_lock);
  953. /* Ring doorbell once to update PCC subspace */
  954. if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
  955. ret = -EIO;
  956. goto out_err;
  957. }
  958. }
  959. cpc_read(cpunum, highest_reg, &high);
  960. perf_caps->highest_perf = high;
  961. cpc_read(cpunum, lowest_reg, &low);
  962. perf_caps->lowest_perf = low;
  963. cpc_read(cpunum, nominal_reg, &nom);
  964. perf_caps->nominal_perf = nom;
  965. cpc_read(cpunum, lowest_non_linear_reg, &min_nonlinear);
  966. perf_caps->lowest_nonlinear_perf = min_nonlinear;
  967. if (!high || !low || !nom || !min_nonlinear)
  968. ret = -EFAULT;
  969. /* Read optional lowest and nominal frequencies if present */
  970. if (CPC_SUPPORTED(low_freq_reg))
  971. cpc_read(cpunum, low_freq_reg, &low_f);
  972. if (CPC_SUPPORTED(nom_freq_reg))
  973. cpc_read(cpunum, nom_freq_reg, &nom_f);
  974. perf_caps->lowest_freq = low_f;
  975. perf_caps->nominal_freq = nom_f;
  976. out_err:
  977. if (regs_in_pcc)
  978. up_write(&pcc_ss_data->pcc_lock);
  979. return ret;
  980. }
  981. EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
  982. /**
  983. * cppc_get_perf_ctrs - Read a CPUs performance feedback counters.
  984. * @cpunum: CPU from which to read counters.
  985. * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
  986. *
  987. * Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
  988. */
  989. int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
  990. {
  991. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  992. struct cpc_register_resource *delivered_reg, *reference_reg,
  993. *ref_perf_reg, *ctr_wrap_reg;
  994. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
  995. struct cppc_pcc_data *pcc_ss_data = NULL;
  996. u64 delivered, reference, ref_perf, ctr_wrap_time;
  997. int ret = 0, regs_in_pcc = 0;
  998. if (!cpc_desc) {
  999. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  1000. return -ENODEV;
  1001. }
  1002. delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
  1003. reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
  1004. ref_perf_reg = &cpc_desc->cpc_regs[REFERENCE_PERF];
  1005. ctr_wrap_reg = &cpc_desc->cpc_regs[CTR_WRAP_TIME];
  1006. /*
  1007. * If refernce perf register is not supported then we should
  1008. * use the nominal perf value
  1009. */
  1010. if (!CPC_SUPPORTED(ref_perf_reg))
  1011. ref_perf_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
  1012. /* Are any of the regs PCC ?*/
  1013. if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) ||
  1014. CPC_IN_PCC(ctr_wrap_reg) || CPC_IN_PCC(ref_perf_reg)) {
  1015. if (pcc_ss_id < 0) {
  1016. pr_debug("Invalid pcc_ss_id\n");
  1017. return -ENODEV;
  1018. }
  1019. pcc_ss_data = pcc_data[pcc_ss_id];
  1020. down_write(&pcc_ss_data->pcc_lock);
  1021. regs_in_pcc = 1;
  1022. /* Ring doorbell once to update PCC subspace */
  1023. if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
  1024. ret = -EIO;
  1025. goto out_err;
  1026. }
  1027. }
  1028. cpc_read(cpunum, delivered_reg, &delivered);
  1029. cpc_read(cpunum, reference_reg, &reference);
  1030. cpc_read(cpunum, ref_perf_reg, &ref_perf);
  1031. /*
  1032. * Per spec, if ctr_wrap_time optional register is unsupported, then the
  1033. * performance counters are assumed to never wrap during the lifetime of
  1034. * platform
  1035. */
  1036. ctr_wrap_time = (u64)(~((u64)0));
  1037. if (CPC_SUPPORTED(ctr_wrap_reg))
  1038. cpc_read(cpunum, ctr_wrap_reg, &ctr_wrap_time);
  1039. if (!delivered || !reference || !ref_perf) {
  1040. ret = -EFAULT;
  1041. goto out_err;
  1042. }
  1043. perf_fb_ctrs->delivered = delivered;
  1044. perf_fb_ctrs->reference = reference;
  1045. perf_fb_ctrs->reference_perf = ref_perf;
  1046. perf_fb_ctrs->wraparound_time = ctr_wrap_time;
  1047. out_err:
  1048. if (regs_in_pcc)
  1049. up_write(&pcc_ss_data->pcc_lock);
  1050. return ret;
  1051. }
  1052. EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
  1053. /**
  1054. * cppc_set_perf - Set a CPUs performance controls.
  1055. * @cpu: CPU for which to set performance controls.
  1056. * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
  1057. *
  1058. * Return: 0 for success, -ERRNO otherwise.
  1059. */
  1060. int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
  1061. {
  1062. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  1063. struct cpc_register_resource *desired_reg;
  1064. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  1065. struct cppc_pcc_data *pcc_ss_data = NULL;
  1066. int ret = 0;
  1067. if (!cpc_desc) {
  1068. pr_debug("No CPC descriptor for CPU:%d\n", cpu);
  1069. return -ENODEV;
  1070. }
  1071. desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
  1072. /*
  1073. * This is Phase-I where we want to write to CPC registers
  1074. * -> We want all CPUs to be able to execute this phase in parallel
  1075. *
  1076. * Since read_lock can be acquired by multiple CPUs simultaneously we
  1077. * achieve that goal here
  1078. */
  1079. if (CPC_IN_PCC(desired_reg)) {
  1080. if (pcc_ss_id < 0) {
  1081. pr_debug("Invalid pcc_ss_id\n");
  1082. return -ENODEV;
  1083. }
  1084. pcc_ss_data = pcc_data[pcc_ss_id];
  1085. down_read(&pcc_ss_data->pcc_lock); /* BEGIN Phase-I */
  1086. if (pcc_ss_data->platform_owns_pcc) {
  1087. ret = check_pcc_chan(pcc_ss_id, false);
  1088. if (ret) {
  1089. up_read(&pcc_ss_data->pcc_lock);
  1090. return ret;
  1091. }
  1092. }
  1093. /*
  1094. * Update the pending_write to make sure a PCC CMD_READ will not
  1095. * arrive and steal the channel during the switch to write lock
  1096. */
  1097. pcc_ss_data->pending_pcc_write_cmd = true;
  1098. cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt;
  1099. cpc_desc->write_cmd_status = 0;
  1100. }
  1101. /*
  1102. * Skip writing MIN/MAX until Linux knows how to come up with
  1103. * useful values.
  1104. */
  1105. cpc_write(cpu, desired_reg, perf_ctrls->desired_perf);
  1106. if (CPC_IN_PCC(desired_reg))
  1107. up_read(&pcc_ss_data->pcc_lock); /* END Phase-I */
  1108. /*
  1109. * This is Phase-II where we transfer the ownership of PCC to Platform
  1110. *
  1111. * Short Summary: Basically if we think of a group of cppc_set_perf
  1112. * requests that happened in short overlapping interval. The last CPU to
  1113. * come out of Phase-I will enter Phase-II and ring the doorbell.
  1114. *
  1115. * We have the following requirements for Phase-II:
  1116. * 1. We want to execute Phase-II only when there are no CPUs
  1117. * currently executing in Phase-I
  1118. * 2. Once we start Phase-II we want to avoid all other CPUs from
  1119. * entering Phase-I.
  1120. * 3. We want only one CPU among all those who went through Phase-I
  1121. * to run phase-II
  1122. *
  1123. * If write_trylock fails to get the lock and doesn't transfer the
  1124. * PCC ownership to the platform, then one of the following will be TRUE
  1125. * 1. There is at-least one CPU in Phase-I which will later execute
  1126. * write_trylock, so the CPUs in Phase-I will be responsible for
  1127. * executing the Phase-II.
  1128. * 2. Some other CPU has beaten this CPU to successfully execute the
  1129. * write_trylock and has already acquired the write_lock. We know for a
  1130. * fact it(other CPU acquiring the write_lock) couldn't have happened
  1131. * before this CPU's Phase-I as we held the read_lock.
  1132. * 3. Some other CPU executing pcc CMD_READ has stolen the
  1133. * down_write, in which case, send_pcc_cmd will check for pending
  1134. * CMD_WRITE commands by checking the pending_pcc_write_cmd.
  1135. * So this CPU can be certain that its request will be delivered
  1136. * So in all cases, this CPU knows that its request will be delivered
  1137. * by another CPU and can return
  1138. *
  1139. * After getting the down_write we still need to check for
  1140. * pending_pcc_write_cmd to take care of the following scenario
  1141. * The thread running this code could be scheduled out between
  1142. * Phase-I and Phase-II. Before it is scheduled back on, another CPU
  1143. * could have delivered the request to Platform by triggering the
  1144. * doorbell and transferred the ownership of PCC to platform. So this
  1145. * avoids triggering an unnecessary doorbell and more importantly before
  1146. * triggering the doorbell it makes sure that the PCC channel ownership
  1147. * is still with OSPM.
  1148. * pending_pcc_write_cmd can also be cleared by a different CPU, if
  1149. * there was a pcc CMD_READ waiting on down_write and it steals the lock
  1150. * before the pcc CMD_WRITE is completed. pcc_send_cmd checks for this
  1151. * case during a CMD_READ and if there are pending writes it delivers
  1152. * the write command before servicing the read command
  1153. */
  1154. if (CPC_IN_PCC(desired_reg)) {
  1155. if (down_write_trylock(&pcc_ss_data->pcc_lock)) {/* BEGIN Phase-II */
  1156. /* Update only if there are pending write commands */
  1157. if (pcc_ss_data->pending_pcc_write_cmd)
  1158. send_pcc_cmd(pcc_ss_id, CMD_WRITE);
  1159. up_write(&pcc_ss_data->pcc_lock); /* END Phase-II */
  1160. } else
  1161. /* Wait until pcc_write_cnt is updated by send_pcc_cmd */
  1162. wait_event(pcc_ss_data->pcc_write_wait_q,
  1163. cpc_desc->write_cmd_id != pcc_ss_data->pcc_write_cnt);
  1164. /* send_pcc_cmd updates the status in case of failure */
  1165. ret = cpc_desc->write_cmd_status;
  1166. }
  1167. return ret;
  1168. }
  1169. EXPORT_SYMBOL_GPL(cppc_set_perf);
  1170. /**
  1171. * cppc_get_transition_latency - returns frequency transition latency in ns
  1172. *
  1173. * ACPI CPPC does not explicitly specifiy how a platform can specify the
  1174. * transition latency for perfromance change requests. The closest we have
  1175. * is the timing information from the PCCT tables which provides the info
  1176. * on the number and frequency of PCC commands the platform can handle.
  1177. */
  1178. unsigned int cppc_get_transition_latency(int cpu_num)
  1179. {
  1180. /*
  1181. * Expected transition latency is based on the PCCT timing values
  1182. * Below are definition from ACPI spec:
  1183. * pcc_nominal- Expected latency to process a command, in microseconds
  1184. * pcc_mpar - The maximum number of periodic requests that the subspace
  1185. * channel can support, reported in commands per minute. 0
  1186. * indicates no limitation.
  1187. * pcc_mrtt - The minimum amount of time that OSPM must wait after the
  1188. * completion of a command before issuing the next command,
  1189. * in microseconds.
  1190. */
  1191. unsigned int latency_ns = 0;
  1192. struct cpc_desc *cpc_desc;
  1193. struct cpc_register_resource *desired_reg;
  1194. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu_num);
  1195. struct cppc_pcc_data *pcc_ss_data;
  1196. cpc_desc = per_cpu(cpc_desc_ptr, cpu_num);
  1197. if (!cpc_desc)
  1198. return CPUFREQ_ETERNAL;
  1199. desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
  1200. if (!CPC_IN_PCC(desired_reg))
  1201. return CPUFREQ_ETERNAL;
  1202. if (pcc_ss_id < 0)
  1203. return CPUFREQ_ETERNAL;
  1204. pcc_ss_data = pcc_data[pcc_ss_id];
  1205. if (pcc_ss_data->pcc_mpar)
  1206. latency_ns = 60 * (1000 * 1000 * 1000 / pcc_ss_data->pcc_mpar);
  1207. latency_ns = max(latency_ns, pcc_ss_data->pcc_nominal * 1000);
  1208. latency_ns = max(latency_ns, pcc_ss_data->pcc_mrtt * 1000);
  1209. return latency_ns;
  1210. }
  1211. EXPORT_SYMBOL_GPL(cppc_get_transition_latency);