cppc_acpi.c 38 KB

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