cppc_acpi.c 40 KB

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