cppc_acpi.c 36 KB

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