1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
|
/*
* Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/cdev.h>
#include <linux/poll.h>
#include <linux/delay.h>
#include <linux/tegra-ivc.h>
#include <linux/tegra-ivc-instance.h>
#include <soc/tegra/chip-id.h>
#include <soc/tegra/virt/tegra_hv_pm_ctl.h>
#include <soc/tegra/virt/syscalls.h>
#include <soc/tegra/virt/tegra_hv_sysmgr.h>
#define DRV_NAME "tegra_hv_pm_ctl"
#define CHAR_DEV_COUNT 1
#define MAX_GUESTS_NUM 8
struct tegra_hv_pm_ctl {
struct device *dev;
u32 ivc;
struct tegra_hv_ivc_cookie *ivck;
struct class *class;
struct cdev cdev;
dev_t char_devt;
bool char_is_open;
u32 wait_for_guests[MAX_GUESTS_NUM];
u32 wait_for_guests_size;
struct mutex mutex_lock;
wait_queue_head_t wq;
};
int (*tegra_hv_pm_ctl_prepare_shutdown)(void);
/* Global driver data */
static struct tegra_hv_pm_ctl *tegra_hv_pm_ctl_data;
/* Guest ID for state */
static u32 guest_id;
static int tegra_hv_pm_ctl_get_guest_state(u32 vmid, u32 *state);
/*
* For dependency management on System suspend, if there are guests required
* to wait and the guests are active, the privileged guest sends
* a guest suspend command to the guests and waits for the guests to be
* suspended or shutsdown. Shutdown is acceptable as the key purpose
* of this function is to ensure this VM stays up while VMs dependent on
* this VM are up
*/
static int do_wait_for_guests_inactive(void)
{
bool sent_guest_suspend = false;
int i = 0;
int ret = 0;
while (i < tegra_hv_pm_ctl_data->wait_for_guests_size) {
u32 vmid = tegra_hv_pm_ctl_data->wait_for_guests[i];
u32 state;
ret = tegra_hv_pm_ctl_get_guest_state(vmid, &state);
if (ret < 0)
return ret;
if (state == VM_STATE_SUSPEND || state == VM_STATE_SHUTDOWN) {
sent_guest_suspend = false;
i++;
continue;
}
if (sent_guest_suspend == false) {
pr_debug("%s: Send a guest suspend command to guest%u\n",
__func__, vmid);
ret = tegra_hv_pm_ctl_trigger_guest_suspend(vmid);
if (ret < 0)
return ret;
sent_guest_suspend = true;
}
msleep(10);
}
return 0;
}
int tegra_hv_pm_ctl_trigger_sys_suspend(void)
{
int ret;
if (!tegra_hv_pm_ctl_data) {
pr_err("%s: tegra_hv_pm_ctl driver is not probed, %d\n",
__func__, -ENXIO);
return -ENXIO;
}
ret = do_wait_for_guests_inactive();
if (ret < 0) {
pr_err("%s: Failed to wait for guests suspended, %d\n",
__func__, ret);
return ret;
}
ret = hyp_guest_reset(SYS_SUSPEND_INIT_CMD, NULL);
if (ret < 0) {
pr_err("%s: Failed to trigger system suspend, %d\n",
__func__, ret);
return ret;
}
return 0;
}
int tegra_hv_pm_ctl_trigger_sys_shutdown(void)
{
int ret;
if (!tegra_hv_pm_ctl_data) {
pr_err("%s: tegra_hv_pm_ctl driver is not probed, %d\n",
__func__, -ENXIO);
return -ENXIO;
}
if (tegra_hv_pm_ctl_prepare_shutdown) {
ret = tegra_hv_pm_ctl_prepare_shutdown();
if (ret < 0) {
pr_err("%s: Failed to prepare shutdown, %d\n",
__func__, ret);
return ret;
}
}
ret = hyp_guest_reset(SYS_SHUTDOWN_INIT_CMD, NULL);
if (ret < 0) {
pr_err("%s: Failed to trigger system shutdown, %d\n",
__func__, ret);
return ret;
}
return 0;
}
int tegra_hv_pm_ctl_trigger_sys_reboot(void)
{
int ret;
if (!tegra_hv_pm_ctl_data) {
pr_err("%s: tegra_hv_pm_ctl driver is not probed, %d\n",
__func__, -ENXIO);
return -ENXIO;
}
ret = hyp_guest_reset(SYS_REBOOT_INIT_CMD, NULL);
if (ret < 0) {
pr_err("%s: Failed to trigger system reboot, %d\n",
__func__, ret);
return ret;
}
return 0;
}
int tegra_hv_pm_ctl_trigger_guest_suspend(u32 vmid)
{
int ret;
if (!tegra_hv_pm_ctl_data) {
pr_err("%s: tegra_hv_pm_ctl driver is not probed, %d\n",
__func__, -ENXIO);
return -ENXIO;
}
ret = hyp_guest_reset(GUEST_SUSPEND_REQ_CMD(vmid), NULL);
if (ret < 0) {
pr_err("%s: Failed to trigger guest%u suspend, %d\n",
__func__, vmid, ret);
return ret;
}
return 0;
}
int tegra_hv_pm_ctl_trigger_guest_reboot(u32 vmid)
{
int ret;
if (!tegra_hv_pm_ctl_data) {
pr_err("%s: tegra_hv_pm_ctl driver is not probed, %d\n",
__func__, -ENXIO);
return -ENXIO;
}
ret = hyp_guest_reset(GUEST_REBOOT_INIT_CMD(vmid), NULL);
if (ret < 0) {
pr_err("%s: Failed to trigger guest%u suspend, %d\n",
__func__, vmid, ret);
return ret;
}
return 0;
}
int tegra_hv_pm_ctl_trigger_guest_resume(u32 vmid)
{
int ret;
if (!tegra_hv_pm_ctl_data) {
pr_err("%s: tegra_hv_pm_ctl driver is not probed, %d\n",
__func__, -ENXIO);
return -ENXIO;
}
ret = hyp_guest_reset(GUEST_RESUME_INIT_CMD(vmid), NULL);
if (ret < 0) {
pr_err("%s: Failed to trigger guest%u resume, %d\n",
__func__, vmid, ret);
return ret;
}
return 0;
}
static int tegra_hv_pm_ctl_get_guest_state(u32 vmid, u32 *state)
{
int ret;
if (!tegra_hv_pm_ctl_data) {
pr_err("%s: tegra_hv_pm_ctl driver is not probed, %d\n",
__func__, -ENXIO);
return -ENXIO;
}
/* guest state which can be returned:
* VM_STATE_BOOT, VM_STATE_HALT, VM_STATE_SUSPEND, VM_STATE_SHUTDOWN */
ret = hyp_read_guest_state(vmid, state);
if (ret < 0) {
pr_err("%s: Failed to get guest%u state, %d\n",
__func__, vmid, ret);
return ret;
}
return 0;
}
static irqreturn_t tegra_hv_pm_ctl_irq(int irq, void *dev_id)
{
struct tegra_hv_pm_ctl *data = dev_id;
struct ivc *ivc = tegra_hv_ivc_convert_cookie(data->ivck);
/* handle IVC state changes */
if (tegra_ivc_channel_notified(ivc) != 0)
goto out;
if (tegra_ivc_can_read(ivc) || tegra_ivc_can_write(ivc))
wake_up_interruptible_all(&data->wq);
out:
return IRQ_HANDLED;
}
static ssize_t tegra_hv_pm_ctl_read(struct file *filp, char __user *buf,
size_t count, loff_t *ppos)
{
struct tegra_hv_pm_ctl *data = filp->private_data;
struct ivc *ivc = tegra_hv_ivc_convert_cookie(data->ivck);
size_t chunk;
int ret = 0;
if (!tegra_ivc_can_read(ivc)) {
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
ret = wait_event_interruptible(data->wq,
tegra_ivc_can_read(ivc));
if (ret < 0)
return ret;
}
chunk = min_t(size_t, count, data->ivck->frame_size);
mutex_lock(&data->mutex_lock);
ret = tegra_ivc_read_user(ivc, buf, chunk);
mutex_unlock(&data->mutex_lock);
if (ret < 0)
dev_err(data->dev, "%s: Failed to read data from IVC %d, %d\n",
__func__, data->ivc, ret);
return ret;
}
static ssize_t tegra_hv_pm_ctl_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
struct tegra_hv_pm_ctl *data = filp->private_data;
struct ivc *ivc = tegra_hv_ivc_convert_cookie(data->ivck);
size_t chunk;
int ret = 0;
if (!tegra_ivc_can_write(ivc)) {
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
ret = wait_event_interruptible(data->wq,
tegra_ivc_can_write(ivc));
if (ret < 0)
return ret;
}
chunk = min_t(size_t, count, data->ivck->frame_size);
mutex_lock(&data->mutex_lock);
ret = tegra_ivc_write_user(ivc, buf, chunk);
mutex_unlock(&data->mutex_lock);
if (ret < 0)
dev_err(data->dev, "%s: Failed to write data from IVC %d, %d\n",
__func__, data->ivc, ret);
return ret;
}
static unsigned int tegra_hv_pm_ctl_poll(struct file *filp,
struct poll_table_struct *table)
{
struct tegra_hv_pm_ctl *data = filp->private_data;
struct ivc *ivc = tegra_hv_ivc_convert_cookie(data->ivck);
unsigned long req_events = poll_requested_events(table);
unsigned int read_mask = POLLIN | POLLRDNORM;
unsigned int write_mask = POLLOUT | POLLWRNORM;
unsigned int mask = 0;
mutex_lock(&data->mutex_lock);
if (!tegra_ivc_can_read(ivc) && (req_events & read_mask)) {
mutex_unlock(&data->mutex_lock);
poll_wait(filp, &data->wq, table);
mutex_lock(&data->mutex_lock);
}
if (tegra_ivc_can_read(ivc))
mask |= read_mask;
if (tegra_ivc_can_write(ivc))
mask |= write_mask;
mutex_unlock(&data->mutex_lock);
return mask;
}
static int tegra_hv_pm_ctl_open(struct inode *inode, struct file *filp)
{
struct cdev *cdev = inode->i_cdev;
struct tegra_hv_pm_ctl *data =
container_of(cdev, struct tegra_hv_pm_ctl, cdev);
int ret = 0;
mutex_lock(&data->mutex_lock);
if (data->char_is_open) {
ret = -EBUSY;
goto out;
}
data->char_is_open = true;
filp->private_data = data;
out:
mutex_unlock(&data->mutex_lock);
return ret;
}
static int tegra_hv_pm_ctl_release(struct inode *inode, struct file *filp)
{
struct tegra_hv_pm_ctl *data = filp->private_data;
mutex_lock(&data->mutex_lock);
data->char_is_open = false;
filp->private_data = NULL;
mutex_unlock(&data->mutex_lock);
return 0;
}
static const struct file_operations tegra_hv_pm_ctl_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.read = tegra_hv_pm_ctl_read,
.write = tegra_hv_pm_ctl_write,
.poll = tegra_hv_pm_ctl_poll,
.open = tegra_hv_pm_ctl_open,
.release = tegra_hv_pm_ctl_release,
};
static ssize_t ivc_id_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%u\n", data->ivc);
}
static ssize_t ivc_frame_size_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%d\n", data->ivck->frame_size);
}
static ssize_t ivc_nframes_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%d\n", data->ivck->nframes);
}
static ssize_t ivc_peer_vmid_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%d\n", data->ivck->peer_vmid);
}
static ssize_t trigger_sys_suspend_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
unsigned int val;
int ret;
ret = kstrtouint(buf, 0, &val);
if (ret) {
dev_err(data->dev, "%s: Failed to convert string to uint\n",
__func__);
return ret;
}
if (val != 1) {
dev_err(data->dev, "%s: Unsupported value, %u\n",
__func__, val);
return -EINVAL;
}
ret = tegra_hv_pm_ctl_trigger_sys_suspend();
if (ret < 0)
return ret;
return count;
}
static ssize_t trigger_sys_shutdown_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
unsigned int val;
int ret;
ret = kstrtouint(buf, 0, &val);
if (ret) {
dev_err(data->dev, "%s: Failed to convert string to uint\n",
__func__);
return ret;
}
if (val != 1) {
dev_err(data->dev, "%s: Unsupported value, %u\n",
__func__, val);
return -EINVAL;
}
ret = tegra_hv_pm_ctl_trigger_sys_shutdown();
if (ret < 0)
return ret;
return count;
}
static ssize_t trigger_sys_reboot_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
unsigned int val;
int ret;
ret = kstrtouint(buf, 0, &val);
if (ret) {
dev_err(data->dev, "%s: Failed to convert string to uint\n",
__func__);
return ret;
}
if (val != 1) {
dev_err(data->dev, "%s: Unsupported value, %u\n",
__func__, val);
return -EINVAL;
}
ret = tegra_hv_pm_ctl_trigger_sys_reboot();
if (ret < 0)
return ret;
return count;
}
static ssize_t trigger_guest_suspend_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
unsigned int val;
int ret;
ret = kstrtouint(buf, 0, &val);
if (ret) {
dev_err(data->dev, "%s: Failed to convert string to uint\n",
__func__);
return ret;
}
ret = tegra_hv_pm_ctl_trigger_guest_suspend(val);
if (ret < 0)
return ret;
return count;
}
static ssize_t trigger_guest_reboot_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
unsigned int val;
int ret;
ret = kstrtouint(buf, 0, &val);
if (ret) {
dev_err(data->dev, "%s: Failed to convert string to uint\n",
__func__);
return ret;
}
ret = tegra_hv_pm_ctl_trigger_guest_reboot(val);
if (ret < 0)
return ret;
return count;
}
static ssize_t trigger_guest_resume_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
unsigned int val;
int ret;
ret = kstrtouint(buf, 0, &val);
if (ret) {
dev_err(data->dev, "%s: Failed to convert string to uint\n",
__func__);
return ret;
}
ret = tegra_hv_pm_ctl_trigger_guest_resume(val);
if (ret < 0)
return ret;
return count;
}
static ssize_t guest_state_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
u32 state = VM_STATE_BOOT;
u32 vmid;
int ret;
mutex_lock(&data->mutex_lock);
vmid = guest_id;
mutex_unlock(&data->mutex_lock);
ret = tegra_hv_pm_ctl_get_guest_state(vmid, &state);
if (ret < 0)
return ret;
return snprintf(buf, PAGE_SIZE, "guest%u: %u\n", vmid, state);
}
static ssize_t guest_state_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
unsigned int val;
int ret;
ret = kstrtouint(buf, 0, &val);
if (ret) {
dev_err(data->dev, "%s: Failed to convert string to uint\n",
__func__);
return ret;
}
mutex_lock(&data->mutex_lock);
guest_id = val;
mutex_unlock(&data->mutex_lock);
return count;
}
static ssize_t wait_for_guests_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tegra_hv_pm_ctl *data = dev_get_drvdata(dev);
ssize_t count = 0;
int i;
for (i = 0; i < data->wait_for_guests_size; i++) {
count += snprintf(buf + count, PAGE_SIZE - count, "%u ",
data->wait_for_guests[i]);
}
count += snprintf(buf + count, PAGE_SIZE - count, "\n");
return count;
}
static DEVICE_ATTR_RO(ivc_id);
static DEVICE_ATTR_RO(ivc_frame_size);
static DEVICE_ATTR_RO(ivc_nframes);
static DEVICE_ATTR_RO(ivc_peer_vmid);
static DEVICE_ATTR_WO(trigger_sys_suspend);
static DEVICE_ATTR_WO(trigger_sys_shutdown);
static DEVICE_ATTR_WO(trigger_sys_reboot);
static DEVICE_ATTR_WO(trigger_guest_reboot);
static DEVICE_ATTR_WO(trigger_guest_suspend);
static DEVICE_ATTR_WO(trigger_guest_resume);
static DEVICE_ATTR_RW(guest_state);
static DEVICE_ATTR_RO(wait_for_guests);
static struct attribute *tegra_hv_pm_ctl_attributes[] = {
&dev_attr_ivc_id.attr,
&dev_attr_ivc_frame_size.attr,
&dev_attr_ivc_nframes.attr,
&dev_attr_ivc_peer_vmid.attr,
&dev_attr_trigger_sys_suspend.attr,
&dev_attr_trigger_sys_shutdown.attr,
&dev_attr_trigger_sys_reboot.attr,
&dev_attr_trigger_guest_reboot.attr,
&dev_attr_trigger_guest_suspend.attr,
&dev_attr_trigger_guest_resume.attr,
&dev_attr_guest_state.attr,
&dev_attr_wait_for_guests.attr,
NULL
};
static const struct attribute_group tegra_hv_pm_ctl_attr_group = {
.attrs = tegra_hv_pm_ctl_attributes,
};
static int tegra_hv_pm_ctl_setup(struct tegra_hv_pm_ctl *data)
{
struct tegra_hv_ivc_cookie *ivck;
struct device *chr_dev;
int ret;
ivck = tegra_hv_ivc_reserve(NULL, data->ivc, NULL);
if (IS_ERR_OR_NULL(ivck)) {
dev_err(data->dev, "%s: Failed to reserve IVC %d\n",
__func__, data->ivc);
data->ivck = NULL;
return -ENODEV;
}
data->ivck = ivck;
dev_dbg(data->dev,
"%s: IVC %d: irq=%d, peer_vmid=%d, nframes=%d, frame_size=%d\n",
__func__, data->ivc, ivck->irq, ivck->peer_vmid, ivck->nframes,
ivck->frame_size);
ret = devm_request_irq(data->dev, ivck->irq, tegra_hv_pm_ctl_irq,
0, dev_name(data->dev), data);
if (ret < 0) {
dev_err(data->dev, "%s: Failed to request irq %d, %d\n",
__func__, ivck->irq, ret);
goto error;
}
tegra_hv_ivc_channel_reset(ivck);
ret = alloc_chrdev_region(&data->char_devt, 0, CHAR_DEV_COUNT,
DRV_NAME);
if (ret < 0) {
dev_err(data->dev, "%s: Failed to alloc chrdev region, %d\n",
__func__, ret);
goto error;
}
cdev_init(&data->cdev, &tegra_hv_pm_ctl_fops);
data->cdev.owner = THIS_MODULE;
ret = cdev_add(&data->cdev, data->char_devt, 1);
if (ret) {
dev_err(data->dev, "%s: Failed to add cdev, %d\n",
__func__, ret);
goto error_unregister_chrdev_region;
}
chr_dev = device_create(data->class, data->dev, data->char_devt,
data, DRV_NAME);
if (IS_ERR(chr_dev)) {
dev_err(data->dev, "%s: Failed to create device, %ld\n",
__func__, PTR_ERR(chr_dev));
ret = PTR_ERR(chr_dev);
goto error_cdev_del;
}
return 0;
error_cdev_del:
cdev_del(&data->cdev);
error_unregister_chrdev_region:
unregister_chrdev_region(MAJOR(data->char_devt), 1);
error:
if (data->ivck)
tegra_hv_ivc_unreserve(data->ivck);
return ret;
}
static void tegra_hv_pm_ctl_cleanup(struct tegra_hv_pm_ctl *data)
{
device_destroy(data->class, data->char_devt);
cdev_del(&data->cdev);
unregister_chrdev_region(MAJOR(data->char_devt), 1);
if (data->ivck)
tegra_hv_ivc_unreserve(data->ivck);
}
static int tegra_hv_pm_ctl_parse_dt(struct tegra_hv_pm_ctl *data)
{
struct device_node *np = data->dev->of_node;
int ret;
if (!np) {
dev_err(data->dev, "%s: Failed to find device node\n",
__func__);
return -EINVAL;
}
if (of_property_read_u32_index(np, "ivc", 1, &data->ivc)) {
dev_err(data->dev, "%s: Failed to find ivc property in %s\n",
__func__, np->full_name);
return -EINVAL;
}
/* List of guests to wait before sending a System suspend command for
* dependency management. */
ret = of_property_read_variable_u32_array(np, "wait-for-guests",
data->wait_for_guests, 1, MAX_GUESTS_NUM);
if (ret > 0)
data->wait_for_guests_size = ret;
return 0;
}
static int tegra_hv_pm_ctl_probe(struct platform_device *pdev)
{
struct tegra_hv_pm_ctl *data;
int ret;
if (!is_tegra_hypervisor_mode()) {
dev_err(&pdev->dev, "%s: Hypervisor is not present\n",
__func__);
return -ENODEV;
}
data = devm_kzalloc(&pdev->dev, sizeof(struct tegra_hv_pm_ctl),
GFP_KERNEL);
if (!data) {
dev_err(&pdev->dev,
"%s: Failed to alloc memory for driver data\n",
__func__);
return -ENOMEM;
}
data->dev = &pdev->dev;
platform_set_drvdata(pdev, data);
mutex_init(&data->mutex_lock);
init_waitqueue_head(&data->wq);
ret = tegra_hv_pm_ctl_parse_dt(data);
if (ret < 0) {
dev_err(&pdev->dev, "%s: Failed to parse device tree, %d\n",
__func__, ret);
return ret;
}
data->class = class_create(THIS_MODULE, DRV_NAME);
if (IS_ERR(data->class)) {
dev_err(data->dev, "%s: Failed to create class, %ld\n",
__func__, PTR_ERR(data->class));
return PTR_ERR(data->class);
}
ret = sysfs_create_group(&pdev->dev.kobj, &tegra_hv_pm_ctl_attr_group);
if (ret < 0) {
dev_err(&pdev->dev, "%s: Failed to create sysfs group, %d\n",
__func__, ret);
goto error_class_destroy;
}
ret = tegra_hv_pm_ctl_setup(data);
if (ret < 0) {
dev_err(&pdev->dev, "%s: Failed to setup device, %d\n",
__func__, ret);
goto error_sysfs_remove_group;
}
tegra_hv_pm_ctl_data = data;
dev_info(&pdev->dev, "%s: Probed\n", __func__);
return 0;
error_sysfs_remove_group:
sysfs_remove_group(&pdev->dev.kobj, &tegra_hv_pm_ctl_attr_group);
error_class_destroy:
class_destroy(data->class);
return ret;
}
static int tegra_hv_pm_ctl_remove(struct platform_device *pdev)
{
struct tegra_hv_pm_ctl *data = platform_get_drvdata(pdev);
tegra_hv_pm_ctl_data = NULL;
tegra_hv_pm_ctl_cleanup(data);
sysfs_remove_group(&pdev->dev.kobj, &tegra_hv_pm_ctl_attr_group);
class_destroy(data->class);
return 0;
}
static const struct of_device_id tegra_hv_pm_ctl_match[] = {
{ .compatible = "nvidia,tegra-hv-pm-ctl", },
{},
};
MODULE_DEVICE_TABLE(of, tegra_hv_pm_ctl_match);
static struct platform_driver tegra_hv_pm_ctl_driver = {
.driver = {
.name = DRV_NAME,
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(tegra_hv_pm_ctl_match),
},
.probe = tegra_hv_pm_ctl_probe,
.remove = tegra_hv_pm_ctl_remove,
};
module_platform_driver(tegra_hv_pm_ctl_driver);
MODULE_DESCRIPTION("Nvidia hypervisor PM control driver");
MODULE_AUTHOR("Jinyoung Park <jinyoungp@nvidia.com>");
MODULE_LICENSE("GPL");
|