a0e34f62f1108835c5924eb5fd4a0d4df44266ba
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_ras.c
1 /*
2  * Copyright 2018 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  *
23  */
24 #include <linux/debugfs.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include "amdgpu.h"
28 #include "amdgpu_ras.h"
29 #include "amdgpu_atomfirmware.h"
30
31 struct ras_ih_data {
32         /* interrupt bottom half */
33         struct work_struct ih_work;
34         int inuse;
35         /* IP callback */
36         ras_ih_cb cb;
37         /* full of entries */
38         unsigned char *ring;
39         unsigned int ring_size;
40         unsigned int element_size;
41         unsigned int aligned_element_size;
42         unsigned int rptr;
43         unsigned int wptr;
44 };
45
46 struct ras_fs_data {
47         char sysfs_name[32];
48         char debugfs_name[32];
49 };
50
51 struct ras_err_data {
52         unsigned long ue_count;
53         unsigned long ce_count;
54 };
55
56 struct ras_err_handler_data {
57         /* point to bad pages array */
58         struct {
59                 unsigned long bp;
60                 struct amdgpu_bo *bo;
61         } *bps;
62         /* the count of entries */
63         int count;
64         /* the space can place new entries */
65         int space_left;
66         /* last reserved entry's index + 1 */
67         int last_reserved;
68 };
69
70 struct ras_manager {
71         struct ras_common_if head;
72         /* reference count */
73         int use;
74         /* ras block link */
75         struct list_head node;
76         /* the device */
77         struct amdgpu_device *adev;
78         /* debugfs */
79         struct dentry *ent;
80         /* sysfs */
81         struct device_attribute sysfs_attr;
82         int attr_inuse;
83
84         /* fs node name */
85         struct ras_fs_data fs_data;
86
87         /* IH data */
88         struct ras_ih_data ih_data;
89
90         struct ras_err_data err_data;
91 };
92
93 const char *ras_error_string[] = {
94         "none",
95         "parity",
96         "single_correctable",
97         "multi_uncorrectable",
98         "poison",
99 };
100
101 const char *ras_block_string[] = {
102         "umc",
103         "sdma",
104         "gfx",
105         "mmhub",
106         "athub",
107         "pcie_bif",
108         "hdp",
109         "xgmi_wafl",
110         "df",
111         "smn",
112         "sem",
113         "mp0",
114         "mp1",
115         "fuse",
116 };
117
118 #define ras_err_str(i) (ras_error_string[ffs(i)])
119 #define ras_block_str(i) (ras_block_string[i])
120
121 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
122 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
123
124 static void amdgpu_ras_self_test(struct amdgpu_device *adev)
125 {
126         /* TODO */
127 }
128
129 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
130                                         size_t size, loff_t *pos)
131 {
132         struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
133         struct ras_query_if info = {
134                 .head = obj->head,
135         };
136         ssize_t s;
137         char val[128];
138
139         if (amdgpu_ras_error_query(obj->adev, &info))
140                 return -EINVAL;
141
142         s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
143                         "ue", info.ue_count,
144                         "ce", info.ce_count);
145         if (*pos >= s)
146                 return 0;
147
148         s -= *pos;
149         s = min_t(u64, s, size);
150
151
152         if (copy_to_user(buf, &val[*pos], s))
153                 return -EINVAL;
154
155         *pos += s;
156
157         return s;
158 }
159
160 static const struct file_operations amdgpu_ras_debugfs_ops = {
161         .owner = THIS_MODULE,
162         .read = amdgpu_ras_debugfs_read,
163         .write = NULL,
164         .llseek = default_llseek
165 };
166
167 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
168 {
169         int i;
170
171         for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
172                 *block_id = i;
173                 if (strcmp(name, ras_block_str(i)) == 0)
174                         return 0;
175         }
176         return -EINVAL;
177 }
178
179 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
180                 const char __user *buf, size_t size,
181                 loff_t *pos, struct ras_debug_if *data)
182 {
183         ssize_t s = min_t(u64, 64, size);
184         char str[65];
185         char block_name[33];
186         char err[9] = "ue";
187         int op = -1;
188         int block_id;
189         u64 address, value;
190
191         if (*pos)
192                 return -EINVAL;
193         *pos = size;
194
195         memset(str, 0, sizeof(str));
196         memset(data, 0, sizeof(*data));
197
198         if (copy_from_user(str, buf, s))
199                 return -EINVAL;
200
201         if (sscanf(str, "disable %32s", block_name) == 1)
202                 op = 0;
203         else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
204                 op = 1;
205         else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
206                 op = 2;
207         else if (str[0] && str[1] && str[2] && str[3])
208                 /* ascii string, but commands are not matched. */
209                 return -EINVAL;
210
211         if (op != -1) {
212                 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
213                         return -EINVAL;
214
215                 data->head.block = block_id;
216                 data->head.type = memcmp("ue", err, 2) == 0 ?
217                         AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE :
218                         AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
219                 data->op = op;
220
221                 if (op == 2) {
222                         if (sscanf(str, "%*s %*s %*s %llu %llu",
223                                                 &address, &value) != 2)
224                                 if (sscanf(str, "%*s %*s %*s 0x%llx 0x%llx",
225                                                         &address, &value) != 2)
226                                         return -EINVAL;
227                         data->inject.address = address;
228                         data->inject.value = value;
229                 }
230         } else {
231                 if (size < sizeof(*data))
232                         return -EINVAL;
233
234                 if (copy_from_user(data, buf, sizeof(*data)))
235                         return -EINVAL;
236         }
237
238         return 0;
239 }
240 /*
241  * DOC: ras debugfs control interface
242  *
243  * It accepts struct ras_debug_if who has two members.
244  *
245  * First member: ras_debug_if::head or ras_debug_if::inject.
246  *
247  * head is used to indicate which IP block will be under control.
248  *
249  * head has four members, they are block, type, sub_block_index, name.
250  * block: which IP will be under control.
251  * type: what kind of error will be enabled/disabled/injected.
252  * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
253  * name: the name of IP.
254  *
255  * inject has two more members than head, they are address, value.
256  * As their names indicate, inject operation will write the
257  * value to the address.
258  *
259  * Second member: struct ras_debug_if::op.
260  * It has three kinds of operations.
261  *  0: disable RAS on the block. Take ::head as its data.
262  *  1: enable RAS on the block. Take ::head as its data.
263  *  2: inject errors on the block. Take ::inject as its data.
264  *
265  * How to use the interface?
266  * programs:
267  * copy the struct ras_debug_if in your codes and initialize it.
268  * write the struct to the control node.
269  *
270  * bash:
271  * echo op block [error [address value]] > .../ras/ras_ctrl
272  *      op: disable, enable, inject
273  *              disable: only block is needed
274  *              enable: block and error are needed
275  *              inject: error, address, value are needed
276  *      block: umc, smda, gfx, .........
277  *              see ras_block_string[] for details
278  *      error: ue, ce
279  *              ue: multi_uncorrectable
280  *              ce: single_correctable
281  *
282  * here are some examples for bash commands,
283  *      echo inject umc ue 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
284  *      echo inject umc ce 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
285  *      echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
286  *
287  * How to check the result?
288  *
289  * For disable/enable, please check ras features at
290  * /sys/class/drm/card[0/1/2...]/device/ras/features
291  *
292  * For inject, please check corresponding err count at
293  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
294  *
295  * NOTE: operation is only allowed on blocks which are supported.
296  * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
297  */
298 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
299                 size_t size, loff_t *pos)
300 {
301         struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
302         struct ras_debug_if data;
303         int ret = 0;
304
305         ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
306         if (ret)
307                 return -EINVAL;
308
309         if (!amdgpu_ras_is_supported(adev, data.head.block))
310                 return -EINVAL;
311
312         switch (data.op) {
313         case 0:
314                 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
315                 break;
316         case 1:
317                 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
318                 break;
319         case 2:
320                 ret = amdgpu_ras_error_inject(adev, &data.inject);
321                 break;
322         default:
323                 ret = -EINVAL;
324                 break;
325         };
326
327         if (ret)
328                 return -EINVAL;
329
330         return size;
331 }
332
333 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
334         .owner = THIS_MODULE,
335         .read = NULL,
336         .write = amdgpu_ras_debugfs_ctrl_write,
337         .llseek = default_llseek
338 };
339
340 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
341                 struct device_attribute *attr, char *buf)
342 {
343         struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
344         struct ras_query_if info = {
345                 .head = obj->head,
346         };
347
348         if (amdgpu_ras_error_query(obj->adev, &info))
349                 return -EINVAL;
350
351         return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
352                         "ue", info.ue_count,
353                         "ce", info.ce_count);
354 }
355
356 /* obj begin */
357
358 #define get_obj(obj) do { (obj)->use++; } while (0)
359 #define alive_obj(obj) ((obj)->use)
360
361 static inline void put_obj(struct ras_manager *obj)
362 {
363         if (obj && --obj->use == 0)
364                 list_del(&obj->node);
365         if (obj && obj->use < 0) {
366                  DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
367         }
368 }
369
370 /* make one obj and return it. */
371 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
372                 struct ras_common_if *head)
373 {
374         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
375         struct ras_manager *obj;
376
377         if (!con)
378                 return NULL;
379
380         if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
381                 return NULL;
382
383         obj = &con->objs[head->block];
384         /* already exist. return obj? */
385         if (alive_obj(obj))
386                 return NULL;
387
388         obj->head = *head;
389         obj->adev = adev;
390         list_add(&obj->node, &con->head);
391         get_obj(obj);
392
393         return obj;
394 }
395
396 /* return an obj equal to head, or the first when head is NULL */
397 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
398                 struct ras_common_if *head)
399 {
400         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
401         struct ras_manager *obj;
402         int i;
403
404         if (!con)
405                 return NULL;
406
407         if (head) {
408                 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
409                         return NULL;
410
411                 obj = &con->objs[head->block];
412
413                 if (alive_obj(obj)) {
414                         WARN_ON(head->block != obj->head.block);
415                         return obj;
416                 }
417         } else {
418                 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
419                         obj = &con->objs[i];
420                         if (alive_obj(obj)) {
421                                 WARN_ON(i != obj->head.block);
422                                 return obj;
423                         }
424                 }
425         }
426
427         return NULL;
428 }
429 /* obj end */
430
431 /* feature ctl begin */
432 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
433                 struct ras_common_if *head)
434 {
435         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
436
437         return con->hw_supported & BIT(head->block);
438 }
439
440 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
441                 struct ras_common_if *head)
442 {
443         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
444
445         return con->features & BIT(head->block);
446 }
447
448 /*
449  * if obj is not created, then create one.
450  * set feature enable flag.
451  */
452 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
453                 struct ras_common_if *head, int enable)
454 {
455         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
456         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
457
458         /* If hardware does not support ras, then do not create obj.
459          * But if hardware support ras, we can create the obj.
460          * Ras framework checks con->hw_supported to see if it need do
461          * corresponding initialization.
462          * IP checks con->support to see if it need disable ras.
463          */
464         if (!amdgpu_ras_is_feature_allowed(adev, head))
465                 return 0;
466         if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
467                 return 0;
468
469         if (enable) {
470                 if (!obj) {
471                         obj = amdgpu_ras_create_obj(adev, head);
472                         if (!obj)
473                                 return -EINVAL;
474                 } else {
475                         /* In case we create obj somewhere else */
476                         get_obj(obj);
477                 }
478                 con->features |= BIT(head->block);
479         } else {
480                 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
481                         con->features &= ~BIT(head->block);
482                         put_obj(obj);
483                 }
484         }
485
486         return 0;
487 }
488
489 /* wrapper of psp_ras_enable_features */
490 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
491                 struct ras_common_if *head, bool enable)
492 {
493         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
494         union ta_ras_cmd_input info;
495         int ret;
496
497         if (!con)
498                 return -EINVAL;
499
500         if (!enable) {
501                 info.disable_features = (struct ta_ras_disable_features_input) {
502                         .block_id =  amdgpu_ras_block_to_ta(head->block),
503                         .error_type = amdgpu_ras_error_to_ta(head->type),
504                 };
505         } else {
506                 info.enable_features = (struct ta_ras_enable_features_input) {
507                         .block_id =  amdgpu_ras_block_to_ta(head->block),
508                         .error_type = amdgpu_ras_error_to_ta(head->type),
509                 };
510         }
511
512         /* Do not enable if it is not allowed. */
513         WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
514         /* Are we alerady in that state we are going to set? */
515         if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
516                 return 0;
517
518         ret = psp_ras_enable_features(&adev->psp, &info, enable);
519         if (ret) {
520                 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
521                                 enable ? "enable":"disable",
522                                 ras_block_str(head->block),
523                                 ret);
524                 return -EINVAL;
525         }
526
527         /* setup the obj */
528         __amdgpu_ras_feature_enable(adev, head, enable);
529
530         return 0;
531 }
532
533 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
534                 bool bypass)
535 {
536         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
537         struct ras_manager *obj, *tmp;
538
539         list_for_each_entry_safe(obj, tmp, &con->head, node) {
540                 /* bypass psp.
541                  * aka just release the obj and corresponding flags
542                  */
543                 if (bypass) {
544                         if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
545                                 break;
546                 } else {
547                         if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
548                                 break;
549                 }
550         }
551
552         return con->features;
553 }
554
555 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
556                 bool bypass)
557 {
558         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
559         int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
560         int i;
561         const enum amdgpu_ras_error_type default_ras_type =
562                 AMDGPU_RAS_ERROR__NONE;
563
564         for (i = 0; i < ras_block_count; i++) {
565                 struct ras_common_if head = {
566                         .block = i,
567                         .type = default_ras_type,
568                         .sub_block_index = 0,
569                 };
570                 strcpy(head.name, ras_block_str(i));
571                 if (bypass) {
572                         /*
573                          * bypass psp. vbios enable ras for us.
574                          * so just create the obj
575                          */
576                         if (__amdgpu_ras_feature_enable(adev, &head, 1))
577                                 break;
578                 } else {
579                         if (amdgpu_ras_feature_enable(adev, &head, 1))
580                                 break;
581                 }
582         }
583
584         return con->features;
585 }
586 /* feature ctl end */
587
588 /* query/inject/cure begin */
589 int amdgpu_ras_error_query(struct amdgpu_device *adev,
590                 struct ras_query_if *info)
591 {
592         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
593
594         if (!obj)
595                 return -EINVAL;
596         /* TODO might read the register to read the count */
597
598         info->ue_count = obj->err_data.ue_count;
599         info->ce_count = obj->err_data.ce_count;
600
601         return 0;
602 }
603
604 /* wrapper of psp_ras_trigger_error */
605 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
606                 struct ras_inject_if *info)
607 {
608         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
609         struct ta_ras_trigger_error_input block_info = {
610                 .block_id =  amdgpu_ras_block_to_ta(info->head.block),
611                 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
612                 .sub_block_index = info->head.sub_block_index,
613                 .address = info->address,
614                 .value = info->value,
615         };
616         int ret = 0;
617
618         if (!obj)
619                 return -EINVAL;
620
621         ret = psp_ras_trigger_error(&adev->psp, &block_info);
622         if (ret)
623                 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
624                                 ras_block_str(info->head.block),
625                                 ret);
626
627         return ret;
628 }
629
630 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
631                 struct ras_cure_if *info)
632 {
633         /* psp fw has no cure interface for now. */
634         return 0;
635 }
636
637 /* get the total error counts on all IPs */
638 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
639                 bool is_ce)
640 {
641         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
642         struct ras_manager *obj;
643         struct ras_err_data data = {0, 0};
644
645         if (!con)
646                 return -EINVAL;
647
648         list_for_each_entry(obj, &con->head, node) {
649                 struct ras_query_if info = {
650                         .head = obj->head,
651                 };
652
653                 if (amdgpu_ras_error_query(adev, &info))
654                         return -EINVAL;
655
656                 data.ce_count += info.ce_count;
657                 data.ue_count += info.ue_count;
658         }
659
660         return is_ce ? data.ce_count : data.ue_count;
661 }
662 /* query/inject/cure end */
663
664
665 /* sysfs begin */
666
667 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
668                 struct device_attribute *attr, char *buf)
669 {
670         struct amdgpu_ras *con =
671                 container_of(attr, struct amdgpu_ras, features_attr);
672         struct drm_device *ddev = dev_get_drvdata(dev);
673         struct amdgpu_device *adev = ddev->dev_private;
674         struct ras_common_if head;
675         int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
676         int i;
677         ssize_t s;
678         struct ras_manager *obj;
679
680         s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
681
682         for (i = 0; i < ras_block_count; i++) {
683                 head.block = i;
684
685                 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
686                         obj = amdgpu_ras_find_obj(adev, &head);
687                         s += scnprintf(&buf[s], PAGE_SIZE - s,
688                                         "%s: %s\n",
689                                         ras_block_str(i),
690                                         ras_err_str(obj->head.type));
691                 } else
692                         s += scnprintf(&buf[s], PAGE_SIZE - s,
693                                         "%s: disabled\n",
694                                         ras_block_str(i));
695         }
696
697         return s;
698 }
699
700 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
701 {
702         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
703         struct attribute *attrs[] = {
704                 &con->features_attr.attr,
705                 NULL
706         };
707         struct attribute_group group = {
708                 .name = "ras",
709                 .attrs = attrs,
710         };
711
712         con->features_attr = (struct device_attribute) {
713                 .attr = {
714                         .name = "features",
715                         .mode = S_IRUGO,
716                 },
717                         .show = amdgpu_ras_sysfs_features_read,
718         };
719         sysfs_attr_init(attrs[0]);
720
721         return sysfs_create_group(&adev->dev->kobj, &group);
722 }
723
724 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
725 {
726         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
727         struct attribute *attrs[] = {
728                 &con->features_attr.attr,
729                 NULL
730         };
731         struct attribute_group group = {
732                 .name = "ras",
733                 .attrs = attrs,
734         };
735
736         sysfs_remove_group(&adev->dev->kobj, &group);
737
738         return 0;
739 }
740
741 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
742                 struct ras_fs_if *head)
743 {
744         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
745
746         if (!obj || obj->attr_inuse)
747                 return -EINVAL;
748
749         get_obj(obj);
750
751         memcpy(obj->fs_data.sysfs_name,
752                         head->sysfs_name,
753                         sizeof(obj->fs_data.sysfs_name));
754
755         obj->sysfs_attr = (struct device_attribute){
756                 .attr = {
757                         .name = obj->fs_data.sysfs_name,
758                         .mode = S_IRUGO,
759                 },
760                         .show = amdgpu_ras_sysfs_read,
761         };
762         sysfs_attr_init(&obj->sysfs_attr.attr);
763
764         if (sysfs_add_file_to_group(&adev->dev->kobj,
765                                 &obj->sysfs_attr.attr,
766                                 "ras")) {
767                 put_obj(obj);
768                 return -EINVAL;
769         }
770
771         obj->attr_inuse = 1;
772
773         return 0;
774 }
775
776 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
777                 struct ras_common_if *head)
778 {
779         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
780
781         if (!obj || !obj->attr_inuse)
782                 return -EINVAL;
783
784         sysfs_remove_file_from_group(&adev->dev->kobj,
785                                 &obj->sysfs_attr.attr,
786                                 "ras");
787         obj->attr_inuse = 0;
788         put_obj(obj);
789
790         return 0;
791 }
792
793 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
794 {
795         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
796         struct ras_manager *obj, *tmp;
797
798         list_for_each_entry_safe(obj, tmp, &con->head, node) {
799                 amdgpu_ras_sysfs_remove(adev, &obj->head);
800         }
801
802         amdgpu_ras_sysfs_remove_feature_node(adev);
803
804         return 0;
805 }
806 /* sysfs end */
807
808 /* debugfs begin */
809 static int amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
810 {
811         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
812         struct drm_minor *minor = adev->ddev->primary;
813         struct dentry *root = minor->debugfs_root, *dir;
814         struct dentry *ent;
815
816         dir = debugfs_create_dir("ras", root);
817         if (IS_ERR(dir))
818                 return -EINVAL;
819
820         con->dir = dir;
821
822         ent = debugfs_create_file("ras_ctrl",
823                         S_IWUGO | S_IRUGO, con->dir,
824                         adev, &amdgpu_ras_debugfs_ctrl_ops);
825         if (IS_ERR(ent)) {
826                 debugfs_remove(con->dir);
827                 return -EINVAL;
828         }
829
830         con->ent = ent;
831         return 0;
832 }
833
834 int amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
835                 struct ras_fs_if *head)
836 {
837         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
838         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
839         struct dentry *ent;
840
841         if (!obj || obj->ent)
842                 return -EINVAL;
843
844         get_obj(obj);
845
846         memcpy(obj->fs_data.debugfs_name,
847                         head->debugfs_name,
848                         sizeof(obj->fs_data.debugfs_name));
849
850         ent = debugfs_create_file(obj->fs_data.debugfs_name,
851                         S_IWUGO | S_IRUGO, con->dir,
852                         obj, &amdgpu_ras_debugfs_ops);
853
854         if (IS_ERR(ent))
855                 return -EINVAL;
856
857         obj->ent = ent;
858
859         return 0;
860 }
861
862 int amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
863                 struct ras_common_if *head)
864 {
865         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
866
867         if (!obj || !obj->ent)
868                 return 0;
869
870         debugfs_remove(obj->ent);
871         obj->ent = NULL;
872         put_obj(obj);
873
874         return 0;
875 }
876
877 static int amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
878 {
879         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
880         struct ras_manager *obj, *tmp;
881
882         list_for_each_entry_safe(obj, tmp, &con->head, node) {
883                 amdgpu_ras_debugfs_remove(adev, &obj->head);
884         }
885
886         debugfs_remove(con->ent);
887         debugfs_remove(con->dir);
888         con->dir = NULL;
889         con->ent = NULL;
890
891         return 0;
892 }
893 /* debugfs end */
894
895 /* ras fs */
896
897 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
898 {
899         amdgpu_ras_sysfs_create_feature_node(adev);
900         amdgpu_ras_debugfs_create_ctrl_node(adev);
901
902         return 0;
903 }
904
905 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
906 {
907         amdgpu_ras_debugfs_remove_all(adev);
908         amdgpu_ras_sysfs_remove_all(adev);
909         return 0;
910 }
911 /* ras fs end */
912
913 /* ih begin */
914 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
915 {
916         struct ras_ih_data *data = &obj->ih_data;
917         struct amdgpu_iv_entry entry;
918         int ret;
919
920         while (data->rptr != data->wptr) {
921                 rmb();
922                 memcpy(&entry, &data->ring[data->rptr],
923                                 data->element_size);
924
925                 wmb();
926                 data->rptr = (data->aligned_element_size +
927                                 data->rptr) % data->ring_size;
928
929                 /* Let IP handle its data, maybe we need get the output
930                  * from the callback to udpate the error type/count, etc
931                  */
932                 if (data->cb) {
933                         ret = data->cb(obj->adev, &entry);
934                         /* ue will trigger an interrupt, and in that case
935                          * we need do a reset to recovery the whole system.
936                          * But leave IP do that recovery, here we just dispatch
937                          * the error.
938                          */
939                         if (ret == AMDGPU_RAS_UE) {
940                                 obj->err_data.ue_count++;
941                         }
942                         /* Might need get ce count by register, but not all IP
943                          * saves ce count, some IP just use one bit or two bits
944                          * to indicate ce happened.
945                          */
946                 }
947         }
948 }
949
950 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
951 {
952         struct ras_ih_data *data =
953                 container_of(work, struct ras_ih_data, ih_work);
954         struct ras_manager *obj =
955                 container_of(data, struct ras_manager, ih_data);
956
957         amdgpu_ras_interrupt_handler(obj);
958 }
959
960 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
961                 struct ras_dispatch_if *info)
962 {
963         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
964         struct ras_ih_data *data = &obj->ih_data;
965
966         if (!obj)
967                 return -EINVAL;
968
969         if (data->inuse == 0)
970                 return 0;
971
972         /* Might be overflow... */
973         memcpy(&data->ring[data->wptr], info->entry,
974                         data->element_size);
975
976         wmb();
977         data->wptr = (data->aligned_element_size +
978                         data->wptr) % data->ring_size;
979
980         schedule_work(&data->ih_work);
981
982         return 0;
983 }
984
985 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
986                 struct ras_ih_if *info)
987 {
988         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
989         struct ras_ih_data *data;
990
991         if (!obj)
992                 return -EINVAL;
993
994         data = &obj->ih_data;
995         if (data->inuse == 0)
996                 return 0;
997
998         cancel_work_sync(&data->ih_work);
999
1000         kfree(data->ring);
1001         memset(data, 0, sizeof(*data));
1002         put_obj(obj);
1003
1004         return 0;
1005 }
1006
1007 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1008                 struct ras_ih_if *info)
1009 {
1010         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1011         struct ras_ih_data *data;
1012
1013         if (!obj) {
1014                 /* in case we registe the IH before enable ras feature */
1015                 obj = amdgpu_ras_create_obj(adev, &info->head);
1016                 if (!obj)
1017                         return -EINVAL;
1018         } else
1019                 get_obj(obj);
1020
1021         data = &obj->ih_data;
1022         /* add the callback.etc */
1023         *data = (struct ras_ih_data) {
1024                 .inuse = 0,
1025                 .cb = info->cb,
1026                 .element_size = sizeof(struct amdgpu_iv_entry),
1027                 .rptr = 0,
1028                 .wptr = 0,
1029         };
1030
1031         INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1032
1033         data->aligned_element_size = ALIGN(data->element_size, 8);
1034         /* the ring can store 64 iv entries. */
1035         data->ring_size = 64 * data->aligned_element_size;
1036         data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1037         if (!data->ring) {
1038                 put_obj(obj);
1039                 return -ENOMEM;
1040         }
1041
1042         /* IH is ready */
1043         data->inuse = 1;
1044
1045         return 0;
1046 }
1047
1048 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1049 {
1050         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1051         struct ras_manager *obj, *tmp;
1052
1053         list_for_each_entry_safe(obj, tmp, &con->head, node) {
1054                 struct ras_ih_if info = {
1055                         .head = obj->head,
1056                 };
1057                 amdgpu_ras_interrupt_remove_handler(adev, &info);
1058         }
1059
1060         return 0;
1061 }
1062 /* ih end */
1063
1064 /* recovery begin */
1065 static void amdgpu_ras_do_recovery(struct work_struct *work)
1066 {
1067         struct amdgpu_ras *ras =
1068                 container_of(work, struct amdgpu_ras, recovery_work);
1069
1070         amdgpu_device_gpu_recover(ras->adev, 0);
1071         atomic_set(&ras->in_recovery, 0);
1072 }
1073
1074 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1075                 struct amdgpu_bo **bo_ptr)
1076 {
1077         /* no need to free it actually. */
1078         amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1079         return 0;
1080 }
1081
1082 /* reserve vram with size@offset */
1083 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1084                 uint64_t offset, uint64_t size,
1085                 struct amdgpu_bo **bo_ptr)
1086 {
1087         struct ttm_operation_ctx ctx = { false, false };
1088         struct amdgpu_bo_param bp;
1089         int r = 0;
1090         int i;
1091         struct amdgpu_bo *bo;
1092
1093         if (bo_ptr)
1094                 *bo_ptr = NULL;
1095         memset(&bp, 0, sizeof(bp));
1096         bp.size = size;
1097         bp.byte_align = PAGE_SIZE;
1098         bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1099         bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1100                 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1101         bp.type = ttm_bo_type_kernel;
1102         bp.resv = NULL;
1103
1104         r = amdgpu_bo_create(adev, &bp, &bo);
1105         if (r)
1106                 return -EINVAL;
1107
1108         r = amdgpu_bo_reserve(bo, false);
1109         if (r)
1110                 goto error_reserve;
1111
1112         offset = ALIGN(offset, PAGE_SIZE);
1113         for (i = 0; i < bo->placement.num_placement; ++i) {
1114                 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1115                 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1116         }
1117
1118         ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1119         r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1120         if (r)
1121                 goto error_pin;
1122
1123         r = amdgpu_bo_pin_restricted(bo,
1124                         AMDGPU_GEM_DOMAIN_VRAM,
1125                         offset,
1126                         offset + size);
1127         if (r)
1128                 goto error_pin;
1129
1130         if (bo_ptr)
1131                 *bo_ptr = bo;
1132
1133         amdgpu_bo_unreserve(bo);
1134         return r;
1135
1136 error_pin:
1137         amdgpu_bo_unreserve(bo);
1138 error_reserve:
1139         amdgpu_bo_unref(&bo);
1140         return r;
1141 }
1142
1143 /* alloc/realloc bps array */
1144 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1145                 struct ras_err_handler_data *data, int pages)
1146 {
1147         unsigned int old_space = data->count + data->space_left;
1148         unsigned int new_space = old_space + pages;
1149         unsigned int align_space = ALIGN(new_space, 1024);
1150         void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1151
1152         if (!tmp)
1153                 return -ENOMEM;
1154
1155         if (data->bps) {
1156                 memcpy(tmp, data->bps,
1157                                 data->count * sizeof(*data->bps));
1158                 kfree(data->bps);
1159         }
1160
1161         data->bps = tmp;
1162         data->space_left += align_space - old_space;
1163         return 0;
1164 }
1165
1166 /* it deal with vram only. */
1167 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1168                 unsigned long *bps, int pages)
1169 {
1170         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1171         struct ras_err_handler_data *data;
1172         int i = pages;
1173         int ret = 0;
1174
1175         if (!con || !con->eh_data || !bps || pages <= 0)
1176                 return 0;
1177
1178         mutex_lock(&con->recovery_lock);
1179         data = con->eh_data;
1180         if (!data)
1181                 goto out;
1182
1183         if (data->space_left <= pages)
1184                 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1185                         ret = -ENOMEM;
1186                         goto out;
1187                 }
1188
1189         while (i--)
1190                 data->bps[data->count++].bp = bps[i];
1191
1192         data->space_left -= pages;
1193 out:
1194         mutex_unlock(&con->recovery_lock);
1195
1196         return ret;
1197 }
1198
1199 /* called in gpu recovery/init */
1200 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1201 {
1202         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1203         struct ras_err_handler_data *data;
1204         uint64_t bp;
1205         struct amdgpu_bo *bo;
1206         int i;
1207
1208         if (!con || !con->eh_data)
1209                 return 0;
1210
1211         mutex_lock(&con->recovery_lock);
1212         data = con->eh_data;
1213         if (!data)
1214                 goto out;
1215         /* reserve vram at driver post stage. */
1216         for (i = data->last_reserved; i < data->count; i++) {
1217                 bp = data->bps[i].bp;
1218
1219                 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1220                                         PAGE_SIZE, &bo))
1221                         DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1222
1223                 data->bps[i].bo = bo;
1224                 data->last_reserved = i + 1;
1225         }
1226 out:
1227         mutex_unlock(&con->recovery_lock);
1228         return 0;
1229 }
1230
1231 /* called when driver unload */
1232 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1233 {
1234         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1235         struct ras_err_handler_data *data;
1236         struct amdgpu_bo *bo;
1237         int i;
1238
1239         if (!con || !con->eh_data)
1240                 return 0;
1241
1242         mutex_lock(&con->recovery_lock);
1243         data = con->eh_data;
1244         if (!data)
1245                 goto out;
1246
1247         for (i = data->last_reserved - 1; i >= 0; i--) {
1248                 bo = data->bps[i].bo;
1249
1250                 amdgpu_ras_release_vram(adev, &bo);
1251
1252                 data->bps[i].bo = bo;
1253                 data->last_reserved = i;
1254         }
1255 out:
1256         mutex_unlock(&con->recovery_lock);
1257         return 0;
1258 }
1259
1260 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1261 {
1262         /* TODO
1263          * write the array to eeprom when SMU disabled.
1264          */
1265         return 0;
1266 }
1267
1268 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1269 {
1270         /* TODO
1271          * read the array to eeprom when SMU disabled.
1272          */
1273         return 0;
1274 }
1275
1276 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1277 {
1278         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1279         struct ras_err_handler_data **data = &con->eh_data;
1280
1281         *data = kmalloc(sizeof(**data),
1282                         GFP_KERNEL|__GFP_ZERO);
1283         if (!*data)
1284                 return -ENOMEM;
1285
1286         mutex_init(&con->recovery_lock);
1287         INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1288         atomic_set(&con->in_recovery, 0);
1289         con->adev = adev;
1290
1291         amdgpu_ras_load_bad_pages(adev);
1292         amdgpu_ras_reserve_bad_pages(adev);
1293
1294         return 0;
1295 }
1296
1297 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1298 {
1299         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1300         struct ras_err_handler_data *data = con->eh_data;
1301
1302         cancel_work_sync(&con->recovery_work);
1303         amdgpu_ras_save_bad_pages(adev);
1304         amdgpu_ras_release_bad_pages(adev);
1305
1306         mutex_lock(&con->recovery_lock);
1307         con->eh_data = NULL;
1308         kfree(data->bps);
1309         kfree(data);
1310         mutex_unlock(&con->recovery_lock);
1311
1312         return 0;
1313 }
1314 /* recovery end */
1315
1316 /*
1317  * check hardware's ras ability which will be saved in hw_supported.
1318  * if hardware does not support ras, we can skip some ras initializtion and
1319  * forbid some ras operations from IP.
1320  * if software itself, say boot parameter, limit the ras ability. We still
1321  * need allow IP do some limited operations, like disable. In such case,
1322  * we have to initialize ras as normal. but need check if operation is
1323  * allowed or not in each function.
1324  */
1325 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1326                 uint32_t *hw_supported, uint32_t *supported)
1327 {
1328         *hw_supported = 0;
1329         *supported = 0;
1330
1331         if (amdgpu_sriov_vf(adev) ||
1332                         adev->asic_type != CHIP_VEGA20)
1333                 return;
1334
1335         if (adev->is_atom_fw &&
1336                         (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1337                          amdgpu_atomfirmware_sram_ecc_supported(adev)))
1338                 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1339
1340         *supported = amdgpu_ras_enable == 0 ?
1341                                 0 : *hw_supported & amdgpu_ras_mask;
1342 }
1343
1344 int amdgpu_ras_init(struct amdgpu_device *adev)
1345 {
1346         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1347
1348         if (con)
1349                 return 0;
1350
1351         con = kmalloc(sizeof(struct amdgpu_ras) +
1352                         sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1353                         GFP_KERNEL|__GFP_ZERO);
1354         if (!con)
1355                 return -ENOMEM;
1356
1357         con->objs = (struct ras_manager *)(con + 1);
1358
1359         amdgpu_ras_set_context(adev, con);
1360
1361         amdgpu_ras_check_supported(adev, &con->hw_supported,
1362                         &con->supported);
1363         con->features = 0;
1364         INIT_LIST_HEAD(&con->head);
1365         /* Might need get this flag from vbios. */
1366         con->flags = RAS_DEFAULT_FLAGS;
1367
1368         if (amdgpu_ras_recovery_init(adev))
1369                 goto recovery_out;
1370
1371         amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1372
1373         if (amdgpu_ras_fs_init(adev))
1374                 goto fs_out;
1375
1376         amdgpu_ras_self_test(adev);
1377
1378         DRM_INFO("RAS INFO: ras initialized successfully, "
1379                         "hardware ability[%x] ras_mask[%x]\n",
1380                         con->hw_supported, con->supported);
1381         return 0;
1382 fs_out:
1383         amdgpu_ras_recovery_fini(adev);
1384 recovery_out:
1385         amdgpu_ras_set_context(adev, NULL);
1386         kfree(con);
1387
1388         return -EINVAL;
1389 }
1390
1391 /* do some init work after IP late init as dependence */
1392 void amdgpu_ras_post_init(struct amdgpu_device *adev)
1393 {
1394         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1395         struct ras_manager *obj, *tmp;
1396
1397         if (!con)
1398                 return;
1399
1400         if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1401                 /* Set up all other IPs which are not implemented. There is a
1402                  * tricky thing that IP's actual ras error type should be
1403                  * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1404                  * ERROR_NONE make sense anyway.
1405                  */
1406                 amdgpu_ras_enable_all_features(adev, 1);
1407
1408                 /* We enable ras on all hw_supported block, but as boot
1409                  * parameter might disable some of them and one or more IP has
1410                  * not implemented yet. So we disable them on behalf.
1411                  */
1412                 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1413                         if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1414                                 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1415                                 /* there should be no any reference. */
1416                                 WARN_ON(alive_obj(obj));
1417                         }
1418                 }
1419         }
1420 }
1421
1422 /* do some fini work before IP fini as dependence */
1423 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1424 {
1425         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1426
1427         if (!con)
1428                 return 0;
1429
1430         /* Need disable ras on all IPs here before ip [hw/sw]fini */
1431         amdgpu_ras_disable_all_features(adev, 0);
1432         amdgpu_ras_recovery_fini(adev);
1433         return 0;
1434 }
1435
1436 int amdgpu_ras_fini(struct amdgpu_device *adev)
1437 {
1438         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1439
1440         if (!con)
1441                 return 0;
1442
1443         amdgpu_ras_fs_fini(adev);
1444         amdgpu_ras_interrupt_remove_all(adev);
1445
1446         WARN(con->features, "Feature mask is not cleared");
1447
1448         if (con->features)
1449                 amdgpu_ras_disable_all_features(adev, 1);
1450
1451         amdgpu_ras_set_context(adev, NULL);
1452         kfree(con);
1453
1454         return 0;
1455 }