cppc_acpi.c 40 KB

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