arm64: dts: qcom: sm8550: add TRNG node
[linux-modified.git] / sound / pci / asihpi / hpioctl.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*******************************************************************************
3     AudioScience HPI driver
4     Common Linux HPI ioctl and module probe/remove functions
5
6     Copyright (C) 1997-2014  AudioScience Inc. <support@audioscience.com>
7
8
9 *******************************************************************************/
10 #define SOURCEFILE_NAME "hpioctl.c"
11
12 #include "hpi_internal.h"
13 #include "hpi_version.h"
14 #include "hpimsginit.h"
15 #include "hpidebug.h"
16 #include "hpimsgx.h"
17 #include "hpioctl.h"
18 #include "hpicmn.h"
19
20 #include <linux/fs.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
23 #include <linux/moduleparam.h>
24 #include <linux/uaccess.h>
25 #include <linux/pci.h>
26 #include <linux/stringify.h>
27 #include <linux/module.h>
28 #include <linux/vmalloc.h>
29 #include <linux/nospec.h>
30
31 #ifdef MODULE_FIRMWARE
32 /*(DEBLOBBED)*/
33 #endif
34
35 static int prealloc_stream_buf;
36 module_param(prealloc_stream_buf, int, 0444);
37 MODULE_PARM_DESC(prealloc_stream_buf,
38         "Preallocate size for per-adapter stream buffer");
39
40 /* Allow the debug level to be changed after module load.
41  E.g.   echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
42 */
43 module_param(hpi_debug_level, int, 0644);
44 MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
45
46 /* List of adapters found */
47 static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
48
49 /* Wrapper function to HPI_Message to enable dumping of the
50    message and response types.
51 */
52 static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
53         struct file *file)
54 {
55         if ((phm->adapter_index >= HPI_MAX_ADAPTERS)
56                 && (phm->object != HPI_OBJ_SUBSYSTEM))
57                 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
58         else
59                 hpi_send_recv_ex(phm, phr, file);
60 }
61
62 /* This is called from hpifunc.c functions, called by ALSA
63  * (or other kernel process) In this case there is no file descriptor
64  * available for the message cache code
65  */
66 void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
67 {
68         hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
69 }
70
71 EXPORT_SYMBOL(hpi_send_recv);
72 /* for radio-asihpi */
73
74 int asihpi_hpi_release(struct file *file)
75 {
76         struct hpi_message hm;
77         struct hpi_response hr;
78
79 /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
80         /* close the subsystem just in case the application forgot to. */
81         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
82                 HPI_SUBSYS_CLOSE);
83         hpi_send_recv_ex(&hm, &hr, file);
84         return 0;
85 }
86
87 long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
88 {
89         struct hpi_ioctl_linux __user *phpi_ioctl_data;
90         void __user *puhm;
91         void __user *puhr;
92         union hpi_message_buffer_v1 *hm;
93         union hpi_response_buffer_v1 *hr;
94         u16 msg_size;
95         u16 res_max_size;
96         u32 uncopied_bytes;
97         int err = 0;
98
99         if (cmd != HPI_IOCTL_LINUX)
100                 return -EINVAL;
101
102         hm = kmalloc(sizeof(*hm), GFP_KERNEL);
103         hr = kzalloc(sizeof(*hr), GFP_KERNEL);
104         if (!hm || !hr) {
105                 err = -ENOMEM;
106                 goto out;
107         }
108
109         phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
110
111         /* Read the message and response pointers from user space.  */
112         if (get_user(puhm, &phpi_ioctl_data->phm)
113                 || get_user(puhr, &phpi_ioctl_data->phr)) {
114                 err = -EFAULT;
115                 goto out;
116         }
117
118         /* Now read the message size and data from user space.  */
119         if (get_user(msg_size, (u16 __user *)puhm)) {
120                 err = -EFAULT;
121                 goto out;
122         }
123         if (msg_size > sizeof(*hm))
124                 msg_size = sizeof(*hm);
125
126         /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
127
128         uncopied_bytes = copy_from_user(hm, puhm, msg_size);
129         if (uncopied_bytes) {
130                 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
131                 err = -EFAULT;
132                 goto out;
133         }
134
135         /* Override h.size in case it is changed between two userspace fetches */
136         hm->h.size = msg_size;
137
138         if (get_user(res_max_size, (u16 __user *)puhr)) {
139                 err = -EFAULT;
140                 goto out;
141         }
142         /* printk(KERN_INFO "user response size %d\n", res_max_size); */
143         if (res_max_size < sizeof(struct hpi_response_header)) {
144                 HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
145                 err = -EFAULT;
146                 goto out;
147         }
148
149         res_max_size = min_t(size_t, res_max_size, sizeof(*hr));
150
151         switch (hm->h.function) {
152         case HPI_SUBSYS_CREATE_ADAPTER:
153         case HPI_ADAPTER_DELETE:
154                 /* Application must not use these functions! */
155                 hr->h.size = sizeof(hr->h);
156                 hr->h.error = HPI_ERROR_INVALID_OPERATION;
157                 hr->h.function = hm->h.function;
158                 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
159                 if (uncopied_bytes)
160                         err = -EFAULT;
161                 else
162                         err = 0;
163                 goto out;
164         }
165
166         hr->h.size = res_max_size;
167         if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
168                 hpi_send_recv_f(&hm->m0, &hr->r0, file);
169         } else {
170                 u16 __user *ptr = NULL;
171                 u32 size = 0;
172                 /* -1=no data 0=read from user mem, 1=write to user mem */
173                 int wrflag = -1;
174                 struct hpi_adapter *pa = NULL;
175
176                 if (hm->h.adapter_index < ARRAY_SIZE(adapters))
177                         pa = &adapters[array_index_nospec(hm->h.adapter_index,
178                                                           ARRAY_SIZE(adapters))];
179
180                 if (!pa || !pa->adapter || !pa->adapter->type) {
181                         hpi_init_response(&hr->r0, hm->h.object,
182                                 hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
183
184                         uncopied_bytes =
185                                 copy_to_user(puhr, hr, sizeof(hr->h));
186                         if (uncopied_bytes)
187                                 err = -EFAULT;
188                         else
189                                 err = 0;
190                         goto out;
191                 }
192
193                 if (mutex_lock_interruptible(&pa->mutex)) {
194                         err = -EINTR;
195                         goto out;
196                 }
197
198                 /* Dig out any pointers embedded in the message.  */
199                 switch (hm->h.function) {
200                 case HPI_OSTREAM_WRITE:
201                 case HPI_ISTREAM_READ:{
202                                 /* Yes, sparse, this is correct. */
203                                 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
204                                 size = hm->m0.u.d.u.data.data_size;
205
206                                 /* Allocate buffer according to application request.
207                                    ?Is it better to alloc/free for the duration
208                                    of the transaction?
209                                  */
210                                 if (pa->buffer_size < size) {
211                                         HPI_DEBUG_LOG(DEBUG,
212                                                 "Realloc adapter %d stream "
213                                                 "buffer from %zd to %d\n",
214                                                 hm->h.adapter_index,
215                                                 pa->buffer_size, size);
216                                         if (pa->p_buffer) {
217                                                 pa->buffer_size = 0;
218                                                 vfree(pa->p_buffer);
219                                         }
220                                         pa->p_buffer = vmalloc(size);
221                                         if (pa->p_buffer)
222                                                 pa->buffer_size = size;
223                                         else {
224                                                 HPI_DEBUG_LOG(ERROR,
225                                                         "HPI could not allocate "
226                                                         "stream buffer size %d\n",
227                                                         size);
228
229                                                 mutex_unlock(&pa->mutex);
230                                                 err = -EINVAL;
231                                                 goto out;
232                                         }
233                                 }
234
235                                 hm->m0.u.d.u.data.pb_data = pa->p_buffer;
236                                 if (hm->h.function == HPI_ISTREAM_READ)
237                                         /* from card, WRITE to user mem */
238                                         wrflag = 1;
239                                 else
240                                         wrflag = 0;
241                                 break;
242                         }
243
244                 default:
245                         size = 0;
246                         break;
247                 }
248
249                 if (size && (wrflag == 0)) {
250                         uncopied_bytes =
251                                 copy_from_user(pa->p_buffer, ptr, size);
252                         if (uncopied_bytes)
253                                 HPI_DEBUG_LOG(WARNING,
254                                         "Missed %d of %d "
255                                         "bytes from user\n", uncopied_bytes,
256                                         size);
257                 }
258
259                 hpi_send_recv_f(&hm->m0, &hr->r0, file);
260
261                 if (size && (wrflag == 1)) {
262                         uncopied_bytes =
263                                 copy_to_user(ptr, pa->p_buffer, size);
264                         if (uncopied_bytes)
265                                 HPI_DEBUG_LOG(WARNING,
266                                         "Missed %d of %d " "bytes to user\n",
267                                         uncopied_bytes, size);
268                 }
269
270                 mutex_unlock(&pa->mutex);
271         }
272
273         /* on return response size must be set */
274         /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
275
276         if (!hr->h.size) {
277                 HPI_DEBUG_LOG(ERROR, "response zero size\n");
278                 err = -EFAULT;
279                 goto out;
280         }
281
282         if (hr->h.size > res_max_size) {
283                 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
284                         res_max_size);
285                 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
286                 hr->h.specific_error = hr->h.size;
287                 hr->h.size = sizeof(hr->h);
288         }
289
290         uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
291         if (uncopied_bytes) {
292                 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
293                 err = -EFAULT;
294                 goto out;
295         }
296
297 out:
298         kfree(hm);
299         kfree(hr);
300         return err;
301 }
302
303 static int asihpi_irq_count;
304
305 static irqreturn_t asihpi_isr(int irq, void *dev_id)
306 {
307         struct hpi_adapter *a = dev_id;
308         int handled;
309
310         if (!a->adapter->irq_query_and_clear) {
311                 pr_err("asihpi_isr ASI%04X:%d no handler\n", a->adapter->type,
312                         a->adapter->index);
313                 return IRQ_NONE;
314         }
315
316         handled = a->adapter->irq_query_and_clear(a->adapter, 0);
317
318         if (!handled)
319                 return IRQ_NONE;
320
321         asihpi_irq_count++;
322         /* printk(KERN_INFO "asihpi_isr %d ASI%04X:%d irq handled\n",
323            asihpi_irq_count, a->adapter->type, a->adapter->index); */
324
325         if (a->interrupt_callback)
326                 return IRQ_WAKE_THREAD;
327
328         return IRQ_HANDLED;
329 }
330
331 static irqreturn_t asihpi_isr_thread(int irq, void *dev_id)
332 {
333         struct hpi_adapter *a = dev_id;
334
335         if (a->interrupt_callback)
336                 a->interrupt_callback(a);
337         return IRQ_HANDLED;
338 }
339
340 int asihpi_adapter_probe(struct pci_dev *pci_dev,
341                          const struct pci_device_id *pci_id)
342 {
343         int idx, nm, low_latency_mode = 0, irq_supported = 0;
344         int adapter_index;
345         unsigned int memlen;
346         struct hpi_message hm;
347         struct hpi_response hr;
348         struct hpi_adapter adapter;
349         struct hpi_pci pci = { 0 };
350
351         memset(&adapter, 0, sizeof(adapter));
352
353         dev_printk(KERN_DEBUG, &pci_dev->dev,
354                 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
355                 pci_dev->device, pci_dev->subsystem_vendor,
356                 pci_dev->subsystem_device, pci_dev->devfn);
357
358         if (pcim_enable_device(pci_dev) < 0) {
359                 dev_err(&pci_dev->dev,
360                         "pci_enable_device failed, disabling device\n");
361                 return -EIO;
362         }
363
364         pci_set_master(pci_dev);        /* also sets latency timer if < 16 */
365
366         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
367                 HPI_SUBSYS_CREATE_ADAPTER);
368         hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
369                 HPI_ERROR_PROCESSING_MESSAGE);
370
371         hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
372
373         nm = HPI_MAX_ADAPTER_MEM_SPACES;
374
375         for (idx = 0; idx < nm; idx++) {
376                 HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
377                         &pci_dev->resource[idx]);
378
379                 if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
380                         memlen = pci_resource_len(pci_dev, idx);
381                         pci.ap_mem_base[idx] =
382                                 ioremap(pci_resource_start(pci_dev, idx),
383                                 memlen);
384                         if (!pci.ap_mem_base[idx]) {
385                                 HPI_DEBUG_LOG(ERROR,
386                                         "ioremap failed, aborting\n");
387                                 /* unmap previously mapped pci mem space */
388                                 goto err;
389                         }
390                 }
391         }
392
393         pci.pci_dev = pci_dev;
394         hm.u.s.resource.bus_type = HPI_BUS_PCI;
395         hm.u.s.resource.r.pci = &pci;
396
397         /* call CreateAdapterObject on the relevant hpi module */
398         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
399         if (hr.error)
400                 goto err;
401
402         adapter_index = hr.u.s.adapter_index;
403         adapter.adapter = hpi_find_adapter(adapter_index);
404
405         if (prealloc_stream_buf) {
406                 adapter.p_buffer = vmalloc(prealloc_stream_buf);
407                 if (!adapter.p_buffer) {
408                         HPI_DEBUG_LOG(ERROR,
409                                 "HPI could not allocate "
410                                 "kernel buffer size %d\n",
411                                 prealloc_stream_buf);
412                         goto err;
413                 }
414         }
415
416         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
417                 HPI_ADAPTER_OPEN);
418         hm.adapter_index = adapter.adapter->index;
419         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
420
421         if (hr.error) {
422                 HPI_DEBUG_LOG(ERROR, "HPI_ADAPTER_OPEN failed, aborting\n");
423                 goto err;
424         }
425
426         /* Check if current mode == Low Latency mode */
427         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
428                 HPI_ADAPTER_GET_MODE);
429         hm.adapter_index = adapter.adapter->index;
430         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
431
432         if (!hr.error
433                 && hr.u.ax.mode.adapter_mode == HPI_ADAPTER_MODE_LOW_LATENCY)
434                 low_latency_mode = 1;
435         else
436                 dev_info(&pci_dev->dev,
437                         "Adapter at index %d is not in low latency mode\n",
438                         adapter.adapter->index);
439
440         /* Check if IRQs are supported */
441         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
442                 HPI_ADAPTER_GET_PROPERTY);
443         hm.adapter_index = adapter.adapter->index;
444         hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_SUPPORTS_IRQ;
445         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
446         if (hr.error || !hr.u.ax.property_get.parameter1) {
447                 dev_info(&pci_dev->dev,
448                         "IRQs not supported by adapter at index %d\n",
449                         adapter.adapter->index);
450         } else {
451                 irq_supported = 1;
452         }
453
454         /* WARNING can't init mutex in 'adapter'
455          * and then copy it to adapters[] ?!?!
456          */
457         adapters[adapter_index] = adapter;
458         mutex_init(&adapters[adapter_index].mutex);
459         pci_set_drvdata(pci_dev, &adapters[adapter_index]);
460
461         if (low_latency_mode && irq_supported) {
462                 if (!adapter.adapter->irq_query_and_clear) {
463                         dev_err(&pci_dev->dev,
464                                 "no IRQ handler for adapter %d, aborting\n",
465                                 adapter.adapter->index);
466                         goto err;
467                 }
468
469                 /* Disable IRQ generation on DSP side by setting the rate to 0 */
470                 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
471                         HPI_ADAPTER_SET_PROPERTY);
472                 hm.adapter_index = adapter.adapter->index;
473                 hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
474                 hm.u.ax.property_set.parameter1 = 0;
475                 hm.u.ax.property_set.parameter2 = 0;
476                 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
477                 if (hr.error) {
478                         HPI_DEBUG_LOG(ERROR,
479                                 "HPI_ADAPTER_GET_MODE failed, aborting\n");
480                         goto err;
481                 }
482
483                 /* Note: request_irq calls asihpi_isr here */
484                 if (request_threaded_irq(pci_dev->irq, asihpi_isr,
485                                          asihpi_isr_thread, IRQF_SHARED,
486                                          "asihpi", &adapters[adapter_index])) {
487                         dev_err(&pci_dev->dev, "request_irq(%d) failed\n",
488                                 pci_dev->irq);
489                         goto err;
490                 }
491
492                 adapters[adapter_index].interrupt_mode = 1;
493
494                 dev_info(&pci_dev->dev, "using irq %d\n", pci_dev->irq);
495                 adapters[adapter_index].irq = pci_dev->irq;
496         } else {
497                 dev_info(&pci_dev->dev, "using polled mode\n");
498         }
499
500         dev_info(&pci_dev->dev, "probe succeeded for ASI%04X HPI index %d\n",
501                  adapter.adapter->type, adapter_index);
502
503         return 0;
504
505 err:
506         while (--idx >= 0) {
507                 if (pci.ap_mem_base[idx]) {
508                         iounmap(pci.ap_mem_base[idx]);
509                         pci.ap_mem_base[idx] = NULL;
510                 }
511         }
512
513         if (adapter.p_buffer) {
514                 adapter.buffer_size = 0;
515                 vfree(adapter.p_buffer);
516         }
517
518         HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
519         return -ENODEV;
520 }
521
522 void asihpi_adapter_remove(struct pci_dev *pci_dev)
523 {
524         int idx;
525         struct hpi_message hm;
526         struct hpi_response hr;
527         struct hpi_adapter *pa;
528         struct hpi_pci pci;
529
530         pa = pci_get_drvdata(pci_dev);
531         pci = pa->adapter->pci;
532
533         /* Disable IRQ generation on DSP side */
534         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
535                 HPI_ADAPTER_SET_PROPERTY);
536         hm.adapter_index = pa->adapter->index;
537         hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
538         hm.u.ax.property_set.parameter1 = 0;
539         hm.u.ax.property_set.parameter2 = 0;
540         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
541
542         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
543                 HPI_ADAPTER_DELETE);
544         hm.adapter_index = pa->adapter->index;
545         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
546
547         /* unmap PCI memory space, mapped during device init. */
548         for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; ++idx)
549                 iounmap(pci.ap_mem_base[idx]);
550
551         if (pa->irq)
552                 free_irq(pa->irq, pa);
553
554         vfree(pa->p_buffer);
555
556         if (1)
557                 dev_info(&pci_dev->dev,
558                          "remove %04x:%04x,%04x:%04x,%04x, HPI index %d\n",
559                          pci_dev->vendor, pci_dev->device,
560                          pci_dev->subsystem_vendor, pci_dev->subsystem_device,
561                          pci_dev->devfn, pa->adapter->index);
562
563         memset(pa, 0, sizeof(*pa));
564 }
565
566 void __init asihpi_init(void)
567 {
568         struct hpi_message hm;
569         struct hpi_response hr;
570
571         memset(adapters, 0, sizeof(adapters));
572
573         printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
574
575         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
576                 HPI_SUBSYS_DRIVER_LOAD);
577         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
578 }
579
580 void asihpi_exit(void)
581 {
582         struct hpi_message hm;
583         struct hpi_response hr;
584
585         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
586                 HPI_SUBSYS_DRIVER_UNLOAD);
587         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
588 }