cppc_acpi.c 35 KB

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