cppc_acpi.c 35 KB

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