i915_gem.c 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  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 (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_gem_clflush.h"
  32. #include "i915_vgpu.h"
  33. #include "i915_trace.h"
  34. #include "intel_drv.h"
  35. #include "intel_frontbuffer.h"
  36. #include "intel_mocs.h"
  37. #include <linux/dma-fence-array.h>
  38. #include <linux/kthread.h>
  39. #include <linux/reservation.h>
  40. #include <linux/shmem_fs.h>
  41. #include <linux/slab.h>
  42. #include <linux/stop_machine.h>
  43. #include <linux/swap.h>
  44. #include <linux/pci.h>
  45. #include <linux/dma-buf.h>
  46. static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
  47. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  48. {
  49. if (obj->cache_dirty)
  50. return false;
  51. if (!obj->cache_coherent)
  52. return true;
  53. return obj->pin_display;
  54. }
  55. static int
  56. insert_mappable_node(struct i915_ggtt *ggtt,
  57. struct drm_mm_node *node, u32 size)
  58. {
  59. memset(node, 0, sizeof(*node));
  60. return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
  61. size, 0, I915_COLOR_UNEVICTABLE,
  62. 0, ggtt->mappable_end,
  63. DRM_MM_INSERT_LOW);
  64. }
  65. static void
  66. remove_mappable_node(struct drm_mm_node *node)
  67. {
  68. drm_mm_remove_node(node);
  69. }
  70. /* some bookkeeping */
  71. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  72. u64 size)
  73. {
  74. spin_lock(&dev_priv->mm.object_stat_lock);
  75. dev_priv->mm.object_count++;
  76. dev_priv->mm.object_memory += size;
  77. spin_unlock(&dev_priv->mm.object_stat_lock);
  78. }
  79. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  80. u64 size)
  81. {
  82. spin_lock(&dev_priv->mm.object_stat_lock);
  83. dev_priv->mm.object_count--;
  84. dev_priv->mm.object_memory -= size;
  85. spin_unlock(&dev_priv->mm.object_stat_lock);
  86. }
  87. static int
  88. i915_gem_wait_for_error(struct i915_gpu_error *error)
  89. {
  90. int ret;
  91. might_sleep();
  92. /*
  93. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  94. * userspace. If it takes that long something really bad is going on and
  95. * we should simply try to bail out and fail as gracefully as possible.
  96. */
  97. ret = wait_event_interruptible_timeout(error->reset_queue,
  98. !i915_reset_backoff(error),
  99. I915_RESET_TIMEOUT);
  100. if (ret == 0) {
  101. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  102. return -EIO;
  103. } else if (ret < 0) {
  104. return ret;
  105. } else {
  106. return 0;
  107. }
  108. }
  109. int i915_mutex_lock_interruptible(struct drm_device *dev)
  110. {
  111. struct drm_i915_private *dev_priv = to_i915(dev);
  112. int ret;
  113. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  114. if (ret)
  115. return ret;
  116. ret = mutex_lock_interruptible(&dev->struct_mutex);
  117. if (ret)
  118. return ret;
  119. return 0;
  120. }
  121. int
  122. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  123. struct drm_file *file)
  124. {
  125. struct drm_i915_private *dev_priv = to_i915(dev);
  126. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  127. struct drm_i915_gem_get_aperture *args = data;
  128. struct i915_vma *vma;
  129. u64 pinned;
  130. pinned = ggtt->base.reserved;
  131. mutex_lock(&dev->struct_mutex);
  132. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  133. if (i915_vma_is_pinned(vma))
  134. pinned += vma->node.size;
  135. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  136. if (i915_vma_is_pinned(vma))
  137. pinned += vma->node.size;
  138. mutex_unlock(&dev->struct_mutex);
  139. args->aper_size = ggtt->base.total;
  140. args->aper_available_size = args->aper_size - pinned;
  141. return 0;
  142. }
  143. static struct sg_table *
  144. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  145. {
  146. struct address_space *mapping = obj->base.filp->f_mapping;
  147. drm_dma_handle_t *phys;
  148. struct sg_table *st;
  149. struct scatterlist *sg;
  150. char *vaddr;
  151. int i;
  152. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  153. return ERR_PTR(-EINVAL);
  154. /* Always aligning to the object size, allows a single allocation
  155. * to handle all possible callers, and given typical object sizes,
  156. * the alignment of the buddy allocation will naturally match.
  157. */
  158. phys = drm_pci_alloc(obj->base.dev,
  159. obj->base.size,
  160. roundup_pow_of_two(obj->base.size));
  161. if (!phys)
  162. return ERR_PTR(-ENOMEM);
  163. vaddr = phys->vaddr;
  164. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  165. struct page *page;
  166. char *src;
  167. page = shmem_read_mapping_page(mapping, i);
  168. if (IS_ERR(page)) {
  169. st = ERR_CAST(page);
  170. goto err_phys;
  171. }
  172. src = kmap_atomic(page);
  173. memcpy(vaddr, src, PAGE_SIZE);
  174. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  175. kunmap_atomic(src);
  176. put_page(page);
  177. vaddr += PAGE_SIZE;
  178. }
  179. i915_gem_chipset_flush(to_i915(obj->base.dev));
  180. st = kmalloc(sizeof(*st), GFP_KERNEL);
  181. if (!st) {
  182. st = ERR_PTR(-ENOMEM);
  183. goto err_phys;
  184. }
  185. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  186. kfree(st);
  187. st = ERR_PTR(-ENOMEM);
  188. goto err_phys;
  189. }
  190. sg = st->sgl;
  191. sg->offset = 0;
  192. sg->length = obj->base.size;
  193. sg_dma_address(sg) = phys->busaddr;
  194. sg_dma_len(sg) = obj->base.size;
  195. obj->phys_handle = phys;
  196. return st;
  197. err_phys:
  198. drm_pci_free(obj->base.dev, phys);
  199. return st;
  200. }
  201. static void __start_cpu_write(struct drm_i915_gem_object *obj)
  202. {
  203. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  204. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  205. if (cpu_write_needs_clflush(obj))
  206. obj->cache_dirty = true;
  207. }
  208. static void
  209. __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
  210. struct sg_table *pages,
  211. bool needs_clflush)
  212. {
  213. GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
  214. if (obj->mm.madv == I915_MADV_DONTNEED)
  215. obj->mm.dirty = false;
  216. if (needs_clflush &&
  217. (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
  218. !obj->cache_coherent)
  219. drm_clflush_sg(pages);
  220. __start_cpu_write(obj);
  221. }
  222. static void
  223. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
  224. struct sg_table *pages)
  225. {
  226. __i915_gem_object_release_shmem(obj, pages, false);
  227. if (obj->mm.dirty) {
  228. struct address_space *mapping = obj->base.filp->f_mapping;
  229. char *vaddr = obj->phys_handle->vaddr;
  230. int i;
  231. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  232. struct page *page;
  233. char *dst;
  234. page = shmem_read_mapping_page(mapping, i);
  235. if (IS_ERR(page))
  236. continue;
  237. dst = kmap_atomic(page);
  238. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  239. memcpy(dst, vaddr, PAGE_SIZE);
  240. kunmap_atomic(dst);
  241. set_page_dirty(page);
  242. if (obj->mm.madv == I915_MADV_WILLNEED)
  243. mark_page_accessed(page);
  244. put_page(page);
  245. vaddr += PAGE_SIZE;
  246. }
  247. obj->mm.dirty = false;
  248. }
  249. sg_free_table(pages);
  250. kfree(pages);
  251. drm_pci_free(obj->base.dev, obj->phys_handle);
  252. }
  253. static void
  254. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  255. {
  256. i915_gem_object_unpin_pages(obj);
  257. }
  258. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  259. .get_pages = i915_gem_object_get_pages_phys,
  260. .put_pages = i915_gem_object_put_pages_phys,
  261. .release = i915_gem_object_release_phys,
  262. };
  263. static const struct drm_i915_gem_object_ops i915_gem_object_ops;
  264. int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  265. {
  266. struct i915_vma *vma;
  267. LIST_HEAD(still_in_list);
  268. int ret;
  269. lockdep_assert_held(&obj->base.dev->struct_mutex);
  270. /* Closed vma are removed from the obj->vma_list - but they may
  271. * still have an active binding on the object. To remove those we
  272. * must wait for all rendering to complete to the object (as unbinding
  273. * must anyway), and retire the requests.
  274. */
  275. ret = i915_gem_object_wait(obj,
  276. I915_WAIT_INTERRUPTIBLE |
  277. I915_WAIT_LOCKED |
  278. I915_WAIT_ALL,
  279. MAX_SCHEDULE_TIMEOUT,
  280. NULL);
  281. if (ret)
  282. return ret;
  283. i915_gem_retire_requests(to_i915(obj->base.dev));
  284. while ((vma = list_first_entry_or_null(&obj->vma_list,
  285. struct i915_vma,
  286. obj_link))) {
  287. list_move_tail(&vma->obj_link, &still_in_list);
  288. ret = i915_vma_unbind(vma);
  289. if (ret)
  290. break;
  291. }
  292. list_splice(&still_in_list, &obj->vma_list);
  293. return ret;
  294. }
  295. static long
  296. i915_gem_object_wait_fence(struct dma_fence *fence,
  297. unsigned int flags,
  298. long timeout,
  299. struct intel_rps_client *rps)
  300. {
  301. struct drm_i915_gem_request *rq;
  302. BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
  303. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  304. return timeout;
  305. if (!dma_fence_is_i915(fence))
  306. return dma_fence_wait_timeout(fence,
  307. flags & I915_WAIT_INTERRUPTIBLE,
  308. timeout);
  309. rq = to_request(fence);
  310. if (i915_gem_request_completed(rq))
  311. goto out;
  312. /* This client is about to stall waiting for the GPU. In many cases
  313. * this is undesirable and limits the throughput of the system, as
  314. * many clients cannot continue processing user input/output whilst
  315. * blocked. RPS autotuning may take tens of milliseconds to respond
  316. * to the GPU load and thus incurs additional latency for the client.
  317. * We can circumvent that by promoting the GPU frequency to maximum
  318. * before we wait. This makes the GPU throttle up much more quickly
  319. * (good for benchmarks and user experience, e.g. window animations),
  320. * but at a cost of spending more power processing the workload
  321. * (bad for battery). Not all clients even want their results
  322. * immediately and for them we should just let the GPU select its own
  323. * frequency to maximise efficiency. To prevent a single client from
  324. * forcing the clocks too high for the whole system, we only allow
  325. * each client to waitboost once in a busy period.
  326. */
  327. if (rps) {
  328. if (INTEL_GEN(rq->i915) >= 6)
  329. gen6_rps_boost(rq, rps);
  330. else
  331. rps = NULL;
  332. }
  333. timeout = i915_wait_request(rq, flags, timeout);
  334. out:
  335. if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
  336. i915_gem_request_retire_upto(rq);
  337. return timeout;
  338. }
  339. static long
  340. i915_gem_object_wait_reservation(struct reservation_object *resv,
  341. unsigned int flags,
  342. long timeout,
  343. struct intel_rps_client *rps)
  344. {
  345. unsigned int seq = __read_seqcount_begin(&resv->seq);
  346. struct dma_fence *excl;
  347. bool prune_fences = false;
  348. if (flags & I915_WAIT_ALL) {
  349. struct dma_fence **shared;
  350. unsigned int count, i;
  351. int ret;
  352. ret = reservation_object_get_fences_rcu(resv,
  353. &excl, &count, &shared);
  354. if (ret)
  355. return ret;
  356. for (i = 0; i < count; i++) {
  357. timeout = i915_gem_object_wait_fence(shared[i],
  358. flags, timeout,
  359. rps);
  360. if (timeout < 0)
  361. break;
  362. dma_fence_put(shared[i]);
  363. }
  364. for (; i < count; i++)
  365. dma_fence_put(shared[i]);
  366. kfree(shared);
  367. prune_fences = count && timeout >= 0;
  368. } else {
  369. excl = reservation_object_get_excl_rcu(resv);
  370. }
  371. if (excl && timeout >= 0) {
  372. timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps);
  373. prune_fences = timeout >= 0;
  374. }
  375. dma_fence_put(excl);
  376. /* Oportunistically prune the fences iff we know they have *all* been
  377. * signaled and that the reservation object has not been changed (i.e.
  378. * no new fences have been added).
  379. */
  380. if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
  381. if (reservation_object_trylock(resv)) {
  382. if (!__read_seqcount_retry(&resv->seq, seq))
  383. reservation_object_add_excl_fence(resv, NULL);
  384. reservation_object_unlock(resv);
  385. }
  386. }
  387. return timeout;
  388. }
  389. static void __fence_set_priority(struct dma_fence *fence, int prio)
  390. {
  391. struct drm_i915_gem_request *rq;
  392. struct intel_engine_cs *engine;
  393. if (!dma_fence_is_i915(fence))
  394. return;
  395. rq = to_request(fence);
  396. engine = rq->engine;
  397. if (!engine->schedule)
  398. return;
  399. engine->schedule(rq, prio);
  400. }
  401. static void fence_set_priority(struct dma_fence *fence, int prio)
  402. {
  403. /* Recurse once into a fence-array */
  404. if (dma_fence_is_array(fence)) {
  405. struct dma_fence_array *array = to_dma_fence_array(fence);
  406. int i;
  407. for (i = 0; i < array->num_fences; i++)
  408. __fence_set_priority(array->fences[i], prio);
  409. } else {
  410. __fence_set_priority(fence, prio);
  411. }
  412. }
  413. int
  414. i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
  415. unsigned int flags,
  416. int prio)
  417. {
  418. struct dma_fence *excl;
  419. if (flags & I915_WAIT_ALL) {
  420. struct dma_fence **shared;
  421. unsigned int count, i;
  422. int ret;
  423. ret = reservation_object_get_fences_rcu(obj->resv,
  424. &excl, &count, &shared);
  425. if (ret)
  426. return ret;
  427. for (i = 0; i < count; i++) {
  428. fence_set_priority(shared[i], prio);
  429. dma_fence_put(shared[i]);
  430. }
  431. kfree(shared);
  432. } else {
  433. excl = reservation_object_get_excl_rcu(obj->resv);
  434. }
  435. if (excl) {
  436. fence_set_priority(excl, prio);
  437. dma_fence_put(excl);
  438. }
  439. return 0;
  440. }
  441. /**
  442. * Waits for rendering to the object to be completed
  443. * @obj: i915 gem object
  444. * @flags: how to wait (under a lock, for all rendering or just for writes etc)
  445. * @timeout: how long to wait
  446. * @rps: client (user process) to charge for any waitboosting
  447. */
  448. int
  449. i915_gem_object_wait(struct drm_i915_gem_object *obj,
  450. unsigned int flags,
  451. long timeout,
  452. struct intel_rps_client *rps)
  453. {
  454. might_sleep();
  455. #if IS_ENABLED(CONFIG_LOCKDEP)
  456. GEM_BUG_ON(debug_locks &&
  457. !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
  458. !!(flags & I915_WAIT_LOCKED));
  459. #endif
  460. GEM_BUG_ON(timeout < 0);
  461. timeout = i915_gem_object_wait_reservation(obj->resv,
  462. flags, timeout,
  463. rps);
  464. return timeout < 0 ? timeout : 0;
  465. }
  466. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  467. {
  468. struct drm_i915_file_private *fpriv = file->driver_priv;
  469. return &fpriv->rps;
  470. }
  471. static int
  472. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  473. struct drm_i915_gem_pwrite *args,
  474. struct drm_file *file)
  475. {
  476. void *vaddr = obj->phys_handle->vaddr + args->offset;
  477. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  478. /* We manually control the domain here and pretend that it
  479. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  480. */
  481. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  482. if (copy_from_user(vaddr, user_data, args->size))
  483. return -EFAULT;
  484. drm_clflush_virt_range(vaddr, args->size);
  485. i915_gem_chipset_flush(to_i915(obj->base.dev));
  486. intel_fb_obj_flush(obj, ORIGIN_CPU);
  487. return 0;
  488. }
  489. void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
  490. {
  491. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  492. }
  493. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  494. {
  495. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  496. kmem_cache_free(dev_priv->objects, obj);
  497. }
  498. static int
  499. i915_gem_create(struct drm_file *file,
  500. struct drm_i915_private *dev_priv,
  501. uint64_t size,
  502. uint32_t *handle_p)
  503. {
  504. struct drm_i915_gem_object *obj;
  505. int ret;
  506. u32 handle;
  507. size = roundup(size, PAGE_SIZE);
  508. if (size == 0)
  509. return -EINVAL;
  510. /* Allocate the new object */
  511. obj = i915_gem_object_create(dev_priv, size);
  512. if (IS_ERR(obj))
  513. return PTR_ERR(obj);
  514. ret = drm_gem_handle_create(file, &obj->base, &handle);
  515. /* drop reference from allocate - handle holds it now */
  516. i915_gem_object_put(obj);
  517. if (ret)
  518. return ret;
  519. *handle_p = handle;
  520. return 0;
  521. }
  522. int
  523. i915_gem_dumb_create(struct drm_file *file,
  524. struct drm_device *dev,
  525. struct drm_mode_create_dumb *args)
  526. {
  527. /* have to work out size/pitch and return them */
  528. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  529. args->size = args->pitch * args->height;
  530. return i915_gem_create(file, to_i915(dev),
  531. args->size, &args->handle);
  532. }
  533. static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  534. {
  535. return !(obj->cache_level == I915_CACHE_NONE ||
  536. obj->cache_level == I915_CACHE_WT);
  537. }
  538. /**
  539. * Creates a new mm object and returns a handle to it.
  540. * @dev: drm device pointer
  541. * @data: ioctl data blob
  542. * @file: drm file pointer
  543. */
  544. int
  545. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  546. struct drm_file *file)
  547. {
  548. struct drm_i915_private *dev_priv = to_i915(dev);
  549. struct drm_i915_gem_create *args = data;
  550. i915_gem_flush_free_objects(dev_priv);
  551. return i915_gem_create(file, dev_priv,
  552. args->size, &args->handle);
  553. }
  554. static inline enum fb_op_origin
  555. fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
  556. {
  557. return (domain == I915_GEM_DOMAIN_GTT ?
  558. obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
  559. }
  560. static void
  561. flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
  562. {
  563. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  564. if (!(obj->base.write_domain & flush_domains))
  565. return;
  566. /* No actual flushing is required for the GTT write domain. Writes
  567. * to it "immediately" go to main memory as far as we know, so there's
  568. * no chipset flush. It also doesn't land in render cache.
  569. *
  570. * However, we do have to enforce the order so that all writes through
  571. * the GTT land before any writes to the device, such as updates to
  572. * the GATT itself.
  573. *
  574. * We also have to wait a bit for the writes to land from the GTT.
  575. * An uncached read (i.e. mmio) seems to be ideal for the round-trip
  576. * timing. This issue has only been observed when switching quickly
  577. * between GTT writes and CPU reads from inside the kernel on recent hw,
  578. * and it appears to only affect discrete GTT blocks (i.e. on LLC
  579. * system agents we cannot reproduce this behaviour).
  580. */
  581. wmb();
  582. switch (obj->base.write_domain) {
  583. case I915_GEM_DOMAIN_GTT:
  584. if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv)) {
  585. if (intel_runtime_pm_get_if_in_use(dev_priv)) {
  586. spin_lock_irq(&dev_priv->uncore.lock);
  587. POSTING_READ_FW(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
  588. spin_unlock_irq(&dev_priv->uncore.lock);
  589. intel_runtime_pm_put(dev_priv);
  590. }
  591. }
  592. intel_fb_obj_flush(obj,
  593. fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
  594. break;
  595. case I915_GEM_DOMAIN_CPU:
  596. i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
  597. break;
  598. case I915_GEM_DOMAIN_RENDER:
  599. if (gpu_write_needs_clflush(obj))
  600. obj->cache_dirty = true;
  601. break;
  602. }
  603. obj->base.write_domain = 0;
  604. }
  605. static inline int
  606. __copy_to_user_swizzled(char __user *cpu_vaddr,
  607. const char *gpu_vaddr, int gpu_offset,
  608. int length)
  609. {
  610. int ret, cpu_offset = 0;
  611. while (length > 0) {
  612. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  613. int this_length = min(cacheline_end - gpu_offset, length);
  614. int swizzled_gpu_offset = gpu_offset ^ 64;
  615. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  616. gpu_vaddr + swizzled_gpu_offset,
  617. this_length);
  618. if (ret)
  619. return ret + length;
  620. cpu_offset += this_length;
  621. gpu_offset += this_length;
  622. length -= this_length;
  623. }
  624. return 0;
  625. }
  626. static inline int
  627. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  628. const char __user *cpu_vaddr,
  629. int length)
  630. {
  631. int ret, cpu_offset = 0;
  632. while (length > 0) {
  633. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  634. int this_length = min(cacheline_end - gpu_offset, length);
  635. int swizzled_gpu_offset = gpu_offset ^ 64;
  636. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  637. cpu_vaddr + cpu_offset,
  638. this_length);
  639. if (ret)
  640. return ret + length;
  641. cpu_offset += this_length;
  642. gpu_offset += this_length;
  643. length -= this_length;
  644. }
  645. return 0;
  646. }
  647. /*
  648. * Pins the specified object's pages and synchronizes the object with
  649. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  650. * flush the object from the CPU cache.
  651. */
  652. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  653. unsigned int *needs_clflush)
  654. {
  655. int ret;
  656. lockdep_assert_held(&obj->base.dev->struct_mutex);
  657. *needs_clflush = 0;
  658. if (!i915_gem_object_has_struct_page(obj))
  659. return -ENODEV;
  660. ret = i915_gem_object_wait(obj,
  661. I915_WAIT_INTERRUPTIBLE |
  662. I915_WAIT_LOCKED,
  663. MAX_SCHEDULE_TIMEOUT,
  664. NULL);
  665. if (ret)
  666. return ret;
  667. ret = i915_gem_object_pin_pages(obj);
  668. if (ret)
  669. return ret;
  670. if (obj->cache_coherent || !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  671. ret = i915_gem_object_set_to_cpu_domain(obj, false);
  672. if (ret)
  673. goto err_unpin;
  674. else
  675. goto out;
  676. }
  677. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  678. /* If we're not in the cpu read domain, set ourself into the gtt
  679. * read domain and manually flush cachelines (if required). This
  680. * optimizes for the case when the gpu will dirty the data
  681. * anyway again before the next pread happens.
  682. */
  683. if (!obj->cache_dirty &&
  684. !(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  685. *needs_clflush = CLFLUSH_BEFORE;
  686. out:
  687. /* return with the pages pinned */
  688. return 0;
  689. err_unpin:
  690. i915_gem_object_unpin_pages(obj);
  691. return ret;
  692. }
  693. int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
  694. unsigned int *needs_clflush)
  695. {
  696. int ret;
  697. lockdep_assert_held(&obj->base.dev->struct_mutex);
  698. *needs_clflush = 0;
  699. if (!i915_gem_object_has_struct_page(obj))
  700. return -ENODEV;
  701. ret = i915_gem_object_wait(obj,
  702. I915_WAIT_INTERRUPTIBLE |
  703. I915_WAIT_LOCKED |
  704. I915_WAIT_ALL,
  705. MAX_SCHEDULE_TIMEOUT,
  706. NULL);
  707. if (ret)
  708. return ret;
  709. ret = i915_gem_object_pin_pages(obj);
  710. if (ret)
  711. return ret;
  712. if (obj->cache_coherent || !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  713. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  714. if (ret)
  715. goto err_unpin;
  716. else
  717. goto out;
  718. }
  719. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  720. /* If we're not in the cpu write domain, set ourself into the
  721. * gtt write domain and manually flush cachelines (as required).
  722. * This optimizes for the case when the gpu will use the data
  723. * right away and we therefore have to clflush anyway.
  724. */
  725. if (!obj->cache_dirty) {
  726. *needs_clflush |= CLFLUSH_AFTER;
  727. /*
  728. * Same trick applies to invalidate partially written
  729. * cachelines read before writing.
  730. */
  731. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  732. *needs_clflush |= CLFLUSH_BEFORE;
  733. }
  734. out:
  735. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  736. obj->mm.dirty = true;
  737. /* return with the pages pinned */
  738. return 0;
  739. err_unpin:
  740. i915_gem_object_unpin_pages(obj);
  741. return ret;
  742. }
  743. static void
  744. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  745. bool swizzled)
  746. {
  747. if (unlikely(swizzled)) {
  748. unsigned long start = (unsigned long) addr;
  749. unsigned long end = (unsigned long) addr + length;
  750. /* For swizzling simply ensure that we always flush both
  751. * channels. Lame, but simple and it works. Swizzled
  752. * pwrite/pread is far from a hotpath - current userspace
  753. * doesn't use it at all. */
  754. start = round_down(start, 128);
  755. end = round_up(end, 128);
  756. drm_clflush_virt_range((void *)start, end - start);
  757. } else {
  758. drm_clflush_virt_range(addr, length);
  759. }
  760. }
  761. /* Only difference to the fast-path function is that this can handle bit17
  762. * and uses non-atomic copy and kmap functions. */
  763. static int
  764. shmem_pread_slow(struct page *page, int offset, int length,
  765. char __user *user_data,
  766. bool page_do_bit17_swizzling, bool needs_clflush)
  767. {
  768. char *vaddr;
  769. int ret;
  770. vaddr = kmap(page);
  771. if (needs_clflush)
  772. shmem_clflush_swizzled_range(vaddr + offset, length,
  773. page_do_bit17_swizzling);
  774. if (page_do_bit17_swizzling)
  775. ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
  776. else
  777. ret = __copy_to_user(user_data, vaddr + offset, length);
  778. kunmap(page);
  779. return ret ? - EFAULT : 0;
  780. }
  781. static int
  782. shmem_pread(struct page *page, int offset, int length, char __user *user_data,
  783. bool page_do_bit17_swizzling, bool needs_clflush)
  784. {
  785. int ret;
  786. ret = -ENODEV;
  787. if (!page_do_bit17_swizzling) {
  788. char *vaddr = kmap_atomic(page);
  789. if (needs_clflush)
  790. drm_clflush_virt_range(vaddr + offset, length);
  791. ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
  792. kunmap_atomic(vaddr);
  793. }
  794. if (ret == 0)
  795. return 0;
  796. return shmem_pread_slow(page, offset, length, user_data,
  797. page_do_bit17_swizzling, needs_clflush);
  798. }
  799. static int
  800. i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
  801. struct drm_i915_gem_pread *args)
  802. {
  803. char __user *user_data;
  804. u64 remain;
  805. unsigned int obj_do_bit17_swizzling;
  806. unsigned int needs_clflush;
  807. unsigned int idx, offset;
  808. int ret;
  809. obj_do_bit17_swizzling = 0;
  810. if (i915_gem_object_needs_bit17_swizzle(obj))
  811. obj_do_bit17_swizzling = BIT(17);
  812. ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
  813. if (ret)
  814. return ret;
  815. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  816. mutex_unlock(&obj->base.dev->struct_mutex);
  817. if (ret)
  818. return ret;
  819. remain = args->size;
  820. user_data = u64_to_user_ptr(args->data_ptr);
  821. offset = offset_in_page(args->offset);
  822. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  823. struct page *page = i915_gem_object_get_page(obj, idx);
  824. int length;
  825. length = remain;
  826. if (offset + length > PAGE_SIZE)
  827. length = PAGE_SIZE - offset;
  828. ret = shmem_pread(page, offset, length, user_data,
  829. page_to_phys(page) & obj_do_bit17_swizzling,
  830. needs_clflush);
  831. if (ret)
  832. break;
  833. remain -= length;
  834. user_data += length;
  835. offset = 0;
  836. }
  837. i915_gem_obj_finish_shmem_access(obj);
  838. return ret;
  839. }
  840. static inline bool
  841. gtt_user_read(struct io_mapping *mapping,
  842. loff_t base, int offset,
  843. char __user *user_data, int length)
  844. {
  845. void *vaddr;
  846. unsigned long unwritten;
  847. /* We can use the cpu mem copy function because this is X86. */
  848. vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
  849. unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length);
  850. io_mapping_unmap_atomic(vaddr);
  851. if (unwritten) {
  852. vaddr = (void __force *)
  853. io_mapping_map_wc(mapping, base, PAGE_SIZE);
  854. unwritten = copy_to_user(user_data, vaddr + offset, length);
  855. io_mapping_unmap(vaddr);
  856. }
  857. return unwritten;
  858. }
  859. static int
  860. i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
  861. const struct drm_i915_gem_pread *args)
  862. {
  863. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  864. struct i915_ggtt *ggtt = &i915->ggtt;
  865. struct drm_mm_node node;
  866. struct i915_vma *vma;
  867. void __user *user_data;
  868. u64 remain, offset;
  869. int ret;
  870. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  871. if (ret)
  872. return ret;
  873. intel_runtime_pm_get(i915);
  874. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  875. PIN_MAPPABLE | PIN_NONBLOCK);
  876. if (!IS_ERR(vma)) {
  877. node.start = i915_ggtt_offset(vma);
  878. node.allocated = false;
  879. ret = i915_vma_put_fence(vma);
  880. if (ret) {
  881. i915_vma_unpin(vma);
  882. vma = ERR_PTR(ret);
  883. }
  884. }
  885. if (IS_ERR(vma)) {
  886. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  887. if (ret)
  888. goto out_unlock;
  889. GEM_BUG_ON(!node.allocated);
  890. }
  891. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  892. if (ret)
  893. goto out_unpin;
  894. mutex_unlock(&i915->drm.struct_mutex);
  895. user_data = u64_to_user_ptr(args->data_ptr);
  896. remain = args->size;
  897. offset = args->offset;
  898. while (remain > 0) {
  899. /* Operation in this page
  900. *
  901. * page_base = page offset within aperture
  902. * page_offset = offset within page
  903. * page_length = bytes to copy for this page
  904. */
  905. u32 page_base = node.start;
  906. unsigned page_offset = offset_in_page(offset);
  907. unsigned page_length = PAGE_SIZE - page_offset;
  908. page_length = remain < page_length ? remain : page_length;
  909. if (node.allocated) {
  910. wmb();
  911. ggtt->base.insert_page(&ggtt->base,
  912. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  913. node.start, I915_CACHE_NONE, 0);
  914. wmb();
  915. } else {
  916. page_base += offset & PAGE_MASK;
  917. }
  918. if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
  919. user_data, page_length)) {
  920. ret = -EFAULT;
  921. break;
  922. }
  923. remain -= page_length;
  924. user_data += page_length;
  925. offset += page_length;
  926. }
  927. mutex_lock(&i915->drm.struct_mutex);
  928. out_unpin:
  929. if (node.allocated) {
  930. wmb();
  931. ggtt->base.clear_range(&ggtt->base,
  932. node.start, node.size);
  933. remove_mappable_node(&node);
  934. } else {
  935. i915_vma_unpin(vma);
  936. }
  937. out_unlock:
  938. intel_runtime_pm_put(i915);
  939. mutex_unlock(&i915->drm.struct_mutex);
  940. return ret;
  941. }
  942. /**
  943. * Reads data from the object referenced by handle.
  944. * @dev: drm device pointer
  945. * @data: ioctl data blob
  946. * @file: drm file pointer
  947. *
  948. * On error, the contents of *data are undefined.
  949. */
  950. int
  951. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  952. struct drm_file *file)
  953. {
  954. struct drm_i915_gem_pread *args = data;
  955. struct drm_i915_gem_object *obj;
  956. int ret;
  957. if (args->size == 0)
  958. return 0;
  959. if (!access_ok(VERIFY_WRITE,
  960. u64_to_user_ptr(args->data_ptr),
  961. args->size))
  962. return -EFAULT;
  963. obj = i915_gem_object_lookup(file, args->handle);
  964. if (!obj)
  965. return -ENOENT;
  966. /* Bounds check source. */
  967. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  968. ret = -EINVAL;
  969. goto out;
  970. }
  971. trace_i915_gem_object_pread(obj, args->offset, args->size);
  972. ret = i915_gem_object_wait(obj,
  973. I915_WAIT_INTERRUPTIBLE,
  974. MAX_SCHEDULE_TIMEOUT,
  975. to_rps_client(file));
  976. if (ret)
  977. goto out;
  978. ret = i915_gem_object_pin_pages(obj);
  979. if (ret)
  980. goto out;
  981. ret = i915_gem_shmem_pread(obj, args);
  982. if (ret == -EFAULT || ret == -ENODEV)
  983. ret = i915_gem_gtt_pread(obj, args);
  984. i915_gem_object_unpin_pages(obj);
  985. out:
  986. i915_gem_object_put(obj);
  987. return ret;
  988. }
  989. /* This is the fast write path which cannot handle
  990. * page faults in the source data
  991. */
  992. static inline bool
  993. ggtt_write(struct io_mapping *mapping,
  994. loff_t base, int offset,
  995. char __user *user_data, int length)
  996. {
  997. void *vaddr;
  998. unsigned long unwritten;
  999. /* We can use the cpu mem copy function because this is X86. */
  1000. vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
  1001. unwritten = __copy_from_user_inatomic_nocache(vaddr + offset,
  1002. user_data, length);
  1003. io_mapping_unmap_atomic(vaddr);
  1004. if (unwritten) {
  1005. vaddr = (void __force *)
  1006. io_mapping_map_wc(mapping, base, PAGE_SIZE);
  1007. unwritten = copy_from_user(vaddr + offset, user_data, length);
  1008. io_mapping_unmap(vaddr);
  1009. }
  1010. return unwritten;
  1011. }
  1012. /**
  1013. * This is the fast pwrite path, where we copy the data directly from the
  1014. * user into the GTT, uncached.
  1015. * @obj: i915 GEM object
  1016. * @args: pwrite arguments structure
  1017. */
  1018. static int
  1019. i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
  1020. const struct drm_i915_gem_pwrite *args)
  1021. {
  1022. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1023. struct i915_ggtt *ggtt = &i915->ggtt;
  1024. struct drm_mm_node node;
  1025. struct i915_vma *vma;
  1026. u64 remain, offset;
  1027. void __user *user_data;
  1028. int ret;
  1029. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1030. if (ret)
  1031. return ret;
  1032. intel_runtime_pm_get(i915);
  1033. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  1034. PIN_MAPPABLE | PIN_NONBLOCK);
  1035. if (!IS_ERR(vma)) {
  1036. node.start = i915_ggtt_offset(vma);
  1037. node.allocated = false;
  1038. ret = i915_vma_put_fence(vma);
  1039. if (ret) {
  1040. i915_vma_unpin(vma);
  1041. vma = ERR_PTR(ret);
  1042. }
  1043. }
  1044. if (IS_ERR(vma)) {
  1045. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  1046. if (ret)
  1047. goto out_unlock;
  1048. GEM_BUG_ON(!node.allocated);
  1049. }
  1050. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  1051. if (ret)
  1052. goto out_unpin;
  1053. mutex_unlock(&i915->drm.struct_mutex);
  1054. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  1055. user_data = u64_to_user_ptr(args->data_ptr);
  1056. offset = args->offset;
  1057. remain = args->size;
  1058. while (remain) {
  1059. /* Operation in this page
  1060. *
  1061. * page_base = page offset within aperture
  1062. * page_offset = offset within page
  1063. * page_length = bytes to copy for this page
  1064. */
  1065. u32 page_base = node.start;
  1066. unsigned int page_offset = offset_in_page(offset);
  1067. unsigned int page_length = PAGE_SIZE - page_offset;
  1068. page_length = remain < page_length ? remain : page_length;
  1069. if (node.allocated) {
  1070. wmb(); /* flush the write before we modify the GGTT */
  1071. ggtt->base.insert_page(&ggtt->base,
  1072. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  1073. node.start, I915_CACHE_NONE, 0);
  1074. wmb(); /* flush modifications to the GGTT (insert_page) */
  1075. } else {
  1076. page_base += offset & PAGE_MASK;
  1077. }
  1078. /* If we get a fault while copying data, then (presumably) our
  1079. * source page isn't available. Return the error and we'll
  1080. * retry in the slow path.
  1081. * If the object is non-shmem backed, we retry again with the
  1082. * path that handles page fault.
  1083. */
  1084. if (ggtt_write(&ggtt->mappable, page_base, page_offset,
  1085. user_data, page_length)) {
  1086. ret = -EFAULT;
  1087. break;
  1088. }
  1089. remain -= page_length;
  1090. user_data += page_length;
  1091. offset += page_length;
  1092. }
  1093. intel_fb_obj_flush(obj, ORIGIN_CPU);
  1094. mutex_lock(&i915->drm.struct_mutex);
  1095. out_unpin:
  1096. if (node.allocated) {
  1097. wmb();
  1098. ggtt->base.clear_range(&ggtt->base,
  1099. node.start, node.size);
  1100. remove_mappable_node(&node);
  1101. } else {
  1102. i915_vma_unpin(vma);
  1103. }
  1104. out_unlock:
  1105. intel_runtime_pm_put(i915);
  1106. mutex_unlock(&i915->drm.struct_mutex);
  1107. return ret;
  1108. }
  1109. static int
  1110. shmem_pwrite_slow(struct page *page, int offset, int length,
  1111. char __user *user_data,
  1112. bool page_do_bit17_swizzling,
  1113. bool needs_clflush_before,
  1114. bool needs_clflush_after)
  1115. {
  1116. char *vaddr;
  1117. int ret;
  1118. vaddr = kmap(page);
  1119. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  1120. shmem_clflush_swizzled_range(vaddr + offset, length,
  1121. page_do_bit17_swizzling);
  1122. if (page_do_bit17_swizzling)
  1123. ret = __copy_from_user_swizzled(vaddr, offset, user_data,
  1124. length);
  1125. else
  1126. ret = __copy_from_user(vaddr + offset, user_data, length);
  1127. if (needs_clflush_after)
  1128. shmem_clflush_swizzled_range(vaddr + offset, length,
  1129. page_do_bit17_swizzling);
  1130. kunmap(page);
  1131. return ret ? -EFAULT : 0;
  1132. }
  1133. /* Per-page copy function for the shmem pwrite fastpath.
  1134. * Flushes invalid cachelines before writing to the target if
  1135. * needs_clflush_before is set and flushes out any written cachelines after
  1136. * writing if needs_clflush is set.
  1137. */
  1138. static int
  1139. shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
  1140. bool page_do_bit17_swizzling,
  1141. bool needs_clflush_before,
  1142. bool needs_clflush_after)
  1143. {
  1144. int ret;
  1145. ret = -ENODEV;
  1146. if (!page_do_bit17_swizzling) {
  1147. char *vaddr = kmap_atomic(page);
  1148. if (needs_clflush_before)
  1149. drm_clflush_virt_range(vaddr + offset, len);
  1150. ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
  1151. if (needs_clflush_after)
  1152. drm_clflush_virt_range(vaddr + offset, len);
  1153. kunmap_atomic(vaddr);
  1154. }
  1155. if (ret == 0)
  1156. return ret;
  1157. return shmem_pwrite_slow(page, offset, len, user_data,
  1158. page_do_bit17_swizzling,
  1159. needs_clflush_before,
  1160. needs_clflush_after);
  1161. }
  1162. static int
  1163. i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
  1164. const struct drm_i915_gem_pwrite *args)
  1165. {
  1166. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1167. void __user *user_data;
  1168. u64 remain;
  1169. unsigned int obj_do_bit17_swizzling;
  1170. unsigned int partial_cacheline_write;
  1171. unsigned int needs_clflush;
  1172. unsigned int offset, idx;
  1173. int ret;
  1174. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1175. if (ret)
  1176. return ret;
  1177. ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
  1178. mutex_unlock(&i915->drm.struct_mutex);
  1179. if (ret)
  1180. return ret;
  1181. obj_do_bit17_swizzling = 0;
  1182. if (i915_gem_object_needs_bit17_swizzle(obj))
  1183. obj_do_bit17_swizzling = BIT(17);
  1184. /* If we don't overwrite a cacheline completely we need to be
  1185. * careful to have up-to-date data by first clflushing. Don't
  1186. * overcomplicate things and flush the entire patch.
  1187. */
  1188. partial_cacheline_write = 0;
  1189. if (needs_clflush & CLFLUSH_BEFORE)
  1190. partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
  1191. user_data = u64_to_user_ptr(args->data_ptr);
  1192. remain = args->size;
  1193. offset = offset_in_page(args->offset);
  1194. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  1195. struct page *page = i915_gem_object_get_page(obj, idx);
  1196. int length;
  1197. length = remain;
  1198. if (offset + length > PAGE_SIZE)
  1199. length = PAGE_SIZE - offset;
  1200. ret = shmem_pwrite(page, offset, length, user_data,
  1201. page_to_phys(page) & obj_do_bit17_swizzling,
  1202. (offset | length) & partial_cacheline_write,
  1203. needs_clflush & CLFLUSH_AFTER);
  1204. if (ret)
  1205. break;
  1206. remain -= length;
  1207. user_data += length;
  1208. offset = 0;
  1209. }
  1210. intel_fb_obj_flush(obj, ORIGIN_CPU);
  1211. i915_gem_obj_finish_shmem_access(obj);
  1212. return ret;
  1213. }
  1214. /**
  1215. * Writes data to the object referenced by handle.
  1216. * @dev: drm device
  1217. * @data: ioctl data blob
  1218. * @file: drm file
  1219. *
  1220. * On error, the contents of the buffer that were to be modified are undefined.
  1221. */
  1222. int
  1223. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  1224. struct drm_file *file)
  1225. {
  1226. struct drm_i915_gem_pwrite *args = data;
  1227. struct drm_i915_gem_object *obj;
  1228. int ret;
  1229. if (args->size == 0)
  1230. return 0;
  1231. if (!access_ok(VERIFY_READ,
  1232. u64_to_user_ptr(args->data_ptr),
  1233. args->size))
  1234. return -EFAULT;
  1235. obj = i915_gem_object_lookup(file, args->handle);
  1236. if (!obj)
  1237. return -ENOENT;
  1238. /* Bounds check destination. */
  1239. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  1240. ret = -EINVAL;
  1241. goto err;
  1242. }
  1243. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  1244. ret = -ENODEV;
  1245. if (obj->ops->pwrite)
  1246. ret = obj->ops->pwrite(obj, args);
  1247. if (ret != -ENODEV)
  1248. goto err;
  1249. ret = i915_gem_object_wait(obj,
  1250. I915_WAIT_INTERRUPTIBLE |
  1251. I915_WAIT_ALL,
  1252. MAX_SCHEDULE_TIMEOUT,
  1253. to_rps_client(file));
  1254. if (ret)
  1255. goto err;
  1256. ret = i915_gem_object_pin_pages(obj);
  1257. if (ret)
  1258. goto err;
  1259. ret = -EFAULT;
  1260. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  1261. * it would end up going through the fenced access, and we'll get
  1262. * different detiling behavior between reading and writing.
  1263. * pread/pwrite currently are reading and writing from the CPU
  1264. * perspective, requiring manual detiling by the client.
  1265. */
  1266. if (!i915_gem_object_has_struct_page(obj) ||
  1267. cpu_write_needs_clflush(obj))
  1268. /* Note that the gtt paths might fail with non-page-backed user
  1269. * pointers (e.g. gtt mappings when moving data between
  1270. * textures). Fallback to the shmem path in that case.
  1271. */
  1272. ret = i915_gem_gtt_pwrite_fast(obj, args);
  1273. if (ret == -EFAULT || ret == -ENOSPC) {
  1274. if (obj->phys_handle)
  1275. ret = i915_gem_phys_pwrite(obj, args, file);
  1276. else
  1277. ret = i915_gem_shmem_pwrite(obj, args);
  1278. }
  1279. i915_gem_object_unpin_pages(obj);
  1280. err:
  1281. i915_gem_object_put(obj);
  1282. return ret;
  1283. }
  1284. static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
  1285. {
  1286. struct drm_i915_private *i915;
  1287. struct list_head *list;
  1288. struct i915_vma *vma;
  1289. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  1290. if (!i915_vma_is_ggtt(vma))
  1291. break;
  1292. if (i915_vma_is_active(vma))
  1293. continue;
  1294. if (!drm_mm_node_allocated(&vma->node))
  1295. continue;
  1296. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  1297. }
  1298. i915 = to_i915(obj->base.dev);
  1299. list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
  1300. list_move_tail(&obj->global_link, list);
  1301. }
  1302. /**
  1303. * Called when user space prepares to use an object with the CPU, either
  1304. * through the mmap ioctl's mapping or a GTT mapping.
  1305. * @dev: drm device
  1306. * @data: ioctl data blob
  1307. * @file: drm file
  1308. */
  1309. int
  1310. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1311. struct drm_file *file)
  1312. {
  1313. struct drm_i915_gem_set_domain *args = data;
  1314. struct drm_i915_gem_object *obj;
  1315. uint32_t read_domains = args->read_domains;
  1316. uint32_t write_domain = args->write_domain;
  1317. int err;
  1318. /* Only handle setting domains to types used by the CPU. */
  1319. if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
  1320. return -EINVAL;
  1321. /* Having something in the write domain implies it's in the read
  1322. * domain, and only that read domain. Enforce that in the request.
  1323. */
  1324. if (write_domain != 0 && read_domains != write_domain)
  1325. return -EINVAL;
  1326. obj = i915_gem_object_lookup(file, args->handle);
  1327. if (!obj)
  1328. return -ENOENT;
  1329. /* Try to flush the object off the GPU without holding the lock.
  1330. * We will repeat the flush holding the lock in the normal manner
  1331. * to catch cases where we are gazumped.
  1332. */
  1333. err = i915_gem_object_wait(obj,
  1334. I915_WAIT_INTERRUPTIBLE |
  1335. (write_domain ? I915_WAIT_ALL : 0),
  1336. MAX_SCHEDULE_TIMEOUT,
  1337. to_rps_client(file));
  1338. if (err)
  1339. goto out;
  1340. /* Flush and acquire obj->pages so that we are coherent through
  1341. * direct access in memory with previous cached writes through
  1342. * shmemfs and that our cache domain tracking remains valid.
  1343. * For example, if the obj->filp was moved to swap without us
  1344. * being notified and releasing the pages, we would mistakenly
  1345. * continue to assume that the obj remained out of the CPU cached
  1346. * domain.
  1347. */
  1348. err = i915_gem_object_pin_pages(obj);
  1349. if (err)
  1350. goto out;
  1351. err = i915_mutex_lock_interruptible(dev);
  1352. if (err)
  1353. goto out_unpin;
  1354. if (read_domains & I915_GEM_DOMAIN_WC)
  1355. err = i915_gem_object_set_to_wc_domain(obj, write_domain);
  1356. else if (read_domains & I915_GEM_DOMAIN_GTT)
  1357. err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
  1358. else
  1359. err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
  1360. /* And bump the LRU for this access */
  1361. i915_gem_object_bump_inactive_ggtt(obj);
  1362. mutex_unlock(&dev->struct_mutex);
  1363. if (write_domain != 0)
  1364. intel_fb_obj_invalidate(obj,
  1365. fb_write_origin(obj, write_domain));
  1366. out_unpin:
  1367. i915_gem_object_unpin_pages(obj);
  1368. out:
  1369. i915_gem_object_put(obj);
  1370. return err;
  1371. }
  1372. /**
  1373. * Called when user space has done writes to this buffer
  1374. * @dev: drm device
  1375. * @data: ioctl data blob
  1376. * @file: drm file
  1377. */
  1378. int
  1379. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1380. struct drm_file *file)
  1381. {
  1382. struct drm_i915_gem_sw_finish *args = data;
  1383. struct drm_i915_gem_object *obj;
  1384. obj = i915_gem_object_lookup(file, args->handle);
  1385. if (!obj)
  1386. return -ENOENT;
  1387. /* Pinned buffers may be scanout, so flush the cache */
  1388. i915_gem_object_flush_if_display(obj);
  1389. i915_gem_object_put(obj);
  1390. return 0;
  1391. }
  1392. /**
  1393. * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
  1394. * it is mapped to.
  1395. * @dev: drm device
  1396. * @data: ioctl data blob
  1397. * @file: drm file
  1398. *
  1399. * While the mapping holds a reference on the contents of the object, it doesn't
  1400. * imply a ref on the object itself.
  1401. *
  1402. * IMPORTANT:
  1403. *
  1404. * DRM driver writers who look a this function as an example for how to do GEM
  1405. * mmap support, please don't implement mmap support like here. The modern way
  1406. * to implement DRM mmap support is with an mmap offset ioctl (like
  1407. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1408. * That way debug tooling like valgrind will understand what's going on, hiding
  1409. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1410. * does cpu mmaps this way because we didn't know better.
  1411. */
  1412. int
  1413. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1414. struct drm_file *file)
  1415. {
  1416. struct drm_i915_gem_mmap *args = data;
  1417. struct drm_i915_gem_object *obj;
  1418. unsigned long addr;
  1419. if (args->flags & ~(I915_MMAP_WC))
  1420. return -EINVAL;
  1421. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1422. return -ENODEV;
  1423. obj = i915_gem_object_lookup(file, args->handle);
  1424. if (!obj)
  1425. return -ENOENT;
  1426. /* prime objects have no backing filp to GEM mmap
  1427. * pages from.
  1428. */
  1429. if (!obj->base.filp) {
  1430. i915_gem_object_put(obj);
  1431. return -EINVAL;
  1432. }
  1433. addr = vm_mmap(obj->base.filp, 0, args->size,
  1434. PROT_READ | PROT_WRITE, MAP_SHARED,
  1435. args->offset);
  1436. if (args->flags & I915_MMAP_WC) {
  1437. struct mm_struct *mm = current->mm;
  1438. struct vm_area_struct *vma;
  1439. if (down_write_killable(&mm->mmap_sem)) {
  1440. i915_gem_object_put(obj);
  1441. return -EINTR;
  1442. }
  1443. vma = find_vma(mm, addr);
  1444. if (vma)
  1445. vma->vm_page_prot =
  1446. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1447. else
  1448. addr = -ENOMEM;
  1449. up_write(&mm->mmap_sem);
  1450. /* This may race, but that's ok, it only gets set */
  1451. WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
  1452. }
  1453. i915_gem_object_put(obj);
  1454. if (IS_ERR((void *)addr))
  1455. return addr;
  1456. args->addr_ptr = (uint64_t) addr;
  1457. return 0;
  1458. }
  1459. static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
  1460. {
  1461. return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
  1462. }
  1463. /**
  1464. * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
  1465. *
  1466. * A history of the GTT mmap interface:
  1467. *
  1468. * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
  1469. * aligned and suitable for fencing, and still fit into the available
  1470. * mappable space left by the pinned display objects. A classic problem
  1471. * we called the page-fault-of-doom where we would ping-pong between
  1472. * two objects that could not fit inside the GTT and so the memcpy
  1473. * would page one object in at the expense of the other between every
  1474. * single byte.
  1475. *
  1476. * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
  1477. * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
  1478. * object is too large for the available space (or simply too large
  1479. * for the mappable aperture!), a view is created instead and faulted
  1480. * into userspace. (This view is aligned and sized appropriately for
  1481. * fenced access.)
  1482. *
  1483. * 2 - Recognise WC as a separate cache domain so that we can flush the
  1484. * delayed writes via GTT before performing direct access via WC.
  1485. *
  1486. * Restrictions:
  1487. *
  1488. * * snoopable objects cannot be accessed via the GTT. It can cause machine
  1489. * hangs on some architectures, corruption on others. An attempt to service
  1490. * a GTT page fault from a snoopable object will generate a SIGBUS.
  1491. *
  1492. * * the object must be able to fit into RAM (physical memory, though no
  1493. * limited to the mappable aperture).
  1494. *
  1495. *
  1496. * Caveats:
  1497. *
  1498. * * a new GTT page fault will synchronize rendering from the GPU and flush
  1499. * all data to system memory. Subsequent access will not be synchronized.
  1500. *
  1501. * * all mappings are revoked on runtime device suspend.
  1502. *
  1503. * * there are only 8, 16 or 32 fence registers to share between all users
  1504. * (older machines require fence register for display and blitter access
  1505. * as well). Contention of the fence registers will cause the previous users
  1506. * to be unmapped and any new access will generate new page faults.
  1507. *
  1508. * * running out of memory while servicing a fault may generate a SIGBUS,
  1509. * rather than the expected SIGSEGV.
  1510. */
  1511. int i915_gem_mmap_gtt_version(void)
  1512. {
  1513. return 2;
  1514. }
  1515. static inline struct i915_ggtt_view
  1516. compute_partial_view(struct drm_i915_gem_object *obj,
  1517. pgoff_t page_offset,
  1518. unsigned int chunk)
  1519. {
  1520. struct i915_ggtt_view view;
  1521. if (i915_gem_object_is_tiled(obj))
  1522. chunk = roundup(chunk, tile_row_pages(obj));
  1523. view.type = I915_GGTT_VIEW_PARTIAL;
  1524. view.partial.offset = rounddown(page_offset, chunk);
  1525. view.partial.size =
  1526. min_t(unsigned int, chunk,
  1527. (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
  1528. /* If the partial covers the entire object, just create a normal VMA. */
  1529. if (chunk >= obj->base.size >> PAGE_SHIFT)
  1530. view.type = I915_GGTT_VIEW_NORMAL;
  1531. return view;
  1532. }
  1533. /**
  1534. * i915_gem_fault - fault a page into the GTT
  1535. * @vmf: fault info
  1536. *
  1537. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1538. * from userspace. The fault handler takes care of binding the object to
  1539. * the GTT (if needed), allocating and programming a fence register (again,
  1540. * only if needed based on whether the old reg is still valid or the object
  1541. * is tiled) and inserting a new PTE into the faulting process.
  1542. *
  1543. * Note that the faulting process may involve evicting existing objects
  1544. * from the GTT and/or fence registers to make room. So performance may
  1545. * suffer if the GTT working set is large or there are few fence registers
  1546. * left.
  1547. *
  1548. * The current feature set supported by i915_gem_fault() and thus GTT mmaps
  1549. * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
  1550. */
  1551. int i915_gem_fault(struct vm_fault *vmf)
  1552. {
  1553. #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
  1554. struct vm_area_struct *area = vmf->vma;
  1555. struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
  1556. struct drm_device *dev = obj->base.dev;
  1557. struct drm_i915_private *dev_priv = to_i915(dev);
  1558. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1559. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1560. struct i915_vma *vma;
  1561. pgoff_t page_offset;
  1562. unsigned int flags;
  1563. int ret;
  1564. /* We don't use vmf->pgoff since that has the fake offset */
  1565. page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
  1566. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1567. /* Try to flush the object off the GPU first without holding the lock.
  1568. * Upon acquiring the lock, we will perform our sanity checks and then
  1569. * repeat the flush holding the lock in the normal manner to catch cases
  1570. * where we are gazumped.
  1571. */
  1572. ret = i915_gem_object_wait(obj,
  1573. I915_WAIT_INTERRUPTIBLE,
  1574. MAX_SCHEDULE_TIMEOUT,
  1575. NULL);
  1576. if (ret)
  1577. goto err;
  1578. ret = i915_gem_object_pin_pages(obj);
  1579. if (ret)
  1580. goto err;
  1581. intel_runtime_pm_get(dev_priv);
  1582. ret = i915_mutex_lock_interruptible(dev);
  1583. if (ret)
  1584. goto err_rpm;
  1585. /* Access to snoopable pages through the GTT is incoherent. */
  1586. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
  1587. ret = -EFAULT;
  1588. goto err_unlock;
  1589. }
  1590. /* If the object is smaller than a couple of partial vma, it is
  1591. * not worth only creating a single partial vma - we may as well
  1592. * clear enough space for the full object.
  1593. */
  1594. flags = PIN_MAPPABLE;
  1595. if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
  1596. flags |= PIN_NONBLOCK | PIN_NONFAULT;
  1597. /* Now pin it into the GTT as needed */
  1598. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
  1599. if (IS_ERR(vma)) {
  1600. /* Use a partial view if it is bigger than available space */
  1601. struct i915_ggtt_view view =
  1602. compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
  1603. /* Userspace is now writing through an untracked VMA, abandon
  1604. * all hope that the hardware is able to track future writes.
  1605. */
  1606. obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
  1607. vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
  1608. }
  1609. if (IS_ERR(vma)) {
  1610. ret = PTR_ERR(vma);
  1611. goto err_unlock;
  1612. }
  1613. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1614. if (ret)
  1615. goto err_unpin;
  1616. ret = i915_vma_get_fence(vma);
  1617. if (ret)
  1618. goto err_unpin;
  1619. /* Mark as being mmapped into userspace for later revocation */
  1620. assert_rpm_wakelock_held(dev_priv);
  1621. if (list_empty(&obj->userfault_link))
  1622. list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
  1623. /* Finally, remap it using the new GTT offset */
  1624. ret = remap_io_mapping(area,
  1625. area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
  1626. (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
  1627. min_t(u64, vma->size, area->vm_end - area->vm_start),
  1628. &ggtt->mappable);
  1629. err_unpin:
  1630. __i915_vma_unpin(vma);
  1631. err_unlock:
  1632. mutex_unlock(&dev->struct_mutex);
  1633. err_rpm:
  1634. intel_runtime_pm_put(dev_priv);
  1635. i915_gem_object_unpin_pages(obj);
  1636. err:
  1637. switch (ret) {
  1638. case -EIO:
  1639. /*
  1640. * We eat errors when the gpu is terminally wedged to avoid
  1641. * userspace unduly crashing (gl has no provisions for mmaps to
  1642. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1643. * and so needs to be reported.
  1644. */
  1645. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1646. ret = VM_FAULT_SIGBUS;
  1647. break;
  1648. }
  1649. case -EAGAIN:
  1650. /*
  1651. * EAGAIN means the gpu is hung and we'll wait for the error
  1652. * handler to reset everything when re-faulting in
  1653. * i915_mutex_lock_interruptible.
  1654. */
  1655. case 0:
  1656. case -ERESTARTSYS:
  1657. case -EINTR:
  1658. case -EBUSY:
  1659. /*
  1660. * EBUSY is ok: this just means that another thread
  1661. * already did the job.
  1662. */
  1663. ret = VM_FAULT_NOPAGE;
  1664. break;
  1665. case -ENOMEM:
  1666. ret = VM_FAULT_OOM;
  1667. break;
  1668. case -ENOSPC:
  1669. case -EFAULT:
  1670. ret = VM_FAULT_SIGBUS;
  1671. break;
  1672. default:
  1673. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1674. ret = VM_FAULT_SIGBUS;
  1675. break;
  1676. }
  1677. return ret;
  1678. }
  1679. /**
  1680. * i915_gem_release_mmap - remove physical page mappings
  1681. * @obj: obj in question
  1682. *
  1683. * Preserve the reservation of the mmapping with the DRM core code, but
  1684. * relinquish ownership of the pages back to the system.
  1685. *
  1686. * It is vital that we remove the page mapping if we have mapped a tiled
  1687. * object through the GTT and then lose the fence register due to
  1688. * resource pressure. Similarly if the object has been moved out of the
  1689. * aperture, than pages mapped into userspace must be revoked. Removing the
  1690. * mapping will then trigger a page fault on the next user access, allowing
  1691. * fixup by i915_gem_fault().
  1692. */
  1693. void
  1694. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1695. {
  1696. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1697. /* Serialisation between user GTT access and our code depends upon
  1698. * revoking the CPU's PTE whilst the mutex is held. The next user
  1699. * pagefault then has to wait until we release the mutex.
  1700. *
  1701. * Note that RPM complicates somewhat by adding an additional
  1702. * requirement that operations to the GGTT be made holding the RPM
  1703. * wakeref.
  1704. */
  1705. lockdep_assert_held(&i915->drm.struct_mutex);
  1706. intel_runtime_pm_get(i915);
  1707. if (list_empty(&obj->userfault_link))
  1708. goto out;
  1709. list_del_init(&obj->userfault_link);
  1710. drm_vma_node_unmap(&obj->base.vma_node,
  1711. obj->base.dev->anon_inode->i_mapping);
  1712. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1713. * memory transactions from userspace before we return. The TLB
  1714. * flushing implied above by changing the PTE above *should* be
  1715. * sufficient, an extra barrier here just provides us with a bit
  1716. * of paranoid documentation about our requirement to serialise
  1717. * memory writes before touching registers / GSM.
  1718. */
  1719. wmb();
  1720. out:
  1721. intel_runtime_pm_put(i915);
  1722. }
  1723. void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
  1724. {
  1725. struct drm_i915_gem_object *obj, *on;
  1726. int i;
  1727. /*
  1728. * Only called during RPM suspend. All users of the userfault_list
  1729. * must be holding an RPM wakeref to ensure that this can not
  1730. * run concurrently with themselves (and use the struct_mutex for
  1731. * protection between themselves).
  1732. */
  1733. list_for_each_entry_safe(obj, on,
  1734. &dev_priv->mm.userfault_list, userfault_link) {
  1735. list_del_init(&obj->userfault_link);
  1736. drm_vma_node_unmap(&obj->base.vma_node,
  1737. obj->base.dev->anon_inode->i_mapping);
  1738. }
  1739. /* The fence will be lost when the device powers down. If any were
  1740. * in use by hardware (i.e. they are pinned), we should not be powering
  1741. * down! All other fences will be reacquired by the user upon waking.
  1742. */
  1743. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1744. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1745. /* Ideally we want to assert that the fence register is not
  1746. * live at this point (i.e. that no piece of code will be
  1747. * trying to write through fence + GTT, as that both violates
  1748. * our tracking of activity and associated locking/barriers,
  1749. * but also is illegal given that the hw is powered down).
  1750. *
  1751. * Previously we used reg->pin_count as a "liveness" indicator.
  1752. * That is not sufficient, and we need a more fine-grained
  1753. * tool if we want to have a sanity check here.
  1754. */
  1755. if (!reg->vma)
  1756. continue;
  1757. GEM_BUG_ON(!list_empty(&reg->vma->obj->userfault_link));
  1758. reg->dirty = true;
  1759. }
  1760. }
  1761. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1762. {
  1763. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1764. int err;
  1765. err = drm_gem_create_mmap_offset(&obj->base);
  1766. if (likely(!err))
  1767. return 0;
  1768. /* Attempt to reap some mmap space from dead objects */
  1769. do {
  1770. err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
  1771. if (err)
  1772. break;
  1773. i915_gem_drain_freed_objects(dev_priv);
  1774. err = drm_gem_create_mmap_offset(&obj->base);
  1775. if (!err)
  1776. break;
  1777. } while (flush_delayed_work(&dev_priv->gt.retire_work));
  1778. return err;
  1779. }
  1780. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1781. {
  1782. drm_gem_free_mmap_offset(&obj->base);
  1783. }
  1784. int
  1785. i915_gem_mmap_gtt(struct drm_file *file,
  1786. struct drm_device *dev,
  1787. uint32_t handle,
  1788. uint64_t *offset)
  1789. {
  1790. struct drm_i915_gem_object *obj;
  1791. int ret;
  1792. obj = i915_gem_object_lookup(file, handle);
  1793. if (!obj)
  1794. return -ENOENT;
  1795. ret = i915_gem_object_create_mmap_offset(obj);
  1796. if (ret == 0)
  1797. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1798. i915_gem_object_put(obj);
  1799. return ret;
  1800. }
  1801. /**
  1802. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1803. * @dev: DRM device
  1804. * @data: GTT mapping ioctl data
  1805. * @file: GEM object info
  1806. *
  1807. * Simply returns the fake offset to userspace so it can mmap it.
  1808. * The mmap call will end up in drm_gem_mmap(), which will set things
  1809. * up so we can get faults in the handler above.
  1810. *
  1811. * The fault handler will take care of binding the object into the GTT
  1812. * (since it may have been evicted to make room for something), allocating
  1813. * a fence register, and mapping the appropriate aperture address into
  1814. * userspace.
  1815. */
  1816. int
  1817. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1818. struct drm_file *file)
  1819. {
  1820. struct drm_i915_gem_mmap_gtt *args = data;
  1821. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1822. }
  1823. /* Immediately discard the backing storage */
  1824. static void
  1825. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1826. {
  1827. i915_gem_object_free_mmap_offset(obj);
  1828. if (obj->base.filp == NULL)
  1829. return;
  1830. /* Our goal here is to return as much of the memory as
  1831. * is possible back to the system as we are called from OOM.
  1832. * To do this we must instruct the shmfs to drop all of its
  1833. * backing pages, *now*.
  1834. */
  1835. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1836. obj->mm.madv = __I915_MADV_PURGED;
  1837. obj->mm.pages = ERR_PTR(-EFAULT);
  1838. }
  1839. /* Try to discard unwanted pages */
  1840. void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1841. {
  1842. struct address_space *mapping;
  1843. lockdep_assert_held(&obj->mm.lock);
  1844. GEM_BUG_ON(obj->mm.pages);
  1845. switch (obj->mm.madv) {
  1846. case I915_MADV_DONTNEED:
  1847. i915_gem_object_truncate(obj);
  1848. case __I915_MADV_PURGED:
  1849. return;
  1850. }
  1851. if (obj->base.filp == NULL)
  1852. return;
  1853. mapping = obj->base.filp->f_mapping,
  1854. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1855. }
  1856. static void
  1857. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
  1858. struct sg_table *pages)
  1859. {
  1860. struct sgt_iter sgt_iter;
  1861. struct page *page;
  1862. __i915_gem_object_release_shmem(obj, pages, true);
  1863. i915_gem_gtt_finish_pages(obj, pages);
  1864. if (i915_gem_object_needs_bit17_swizzle(obj))
  1865. i915_gem_object_save_bit_17_swizzle(obj, pages);
  1866. for_each_sgt_page(page, sgt_iter, pages) {
  1867. if (obj->mm.dirty)
  1868. set_page_dirty(page);
  1869. if (obj->mm.madv == I915_MADV_WILLNEED)
  1870. mark_page_accessed(page);
  1871. put_page(page);
  1872. }
  1873. obj->mm.dirty = false;
  1874. sg_free_table(pages);
  1875. kfree(pages);
  1876. }
  1877. static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
  1878. {
  1879. struct radix_tree_iter iter;
  1880. void **slot;
  1881. radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
  1882. radix_tree_delete(&obj->mm.get_page.radix, iter.index);
  1883. }
  1884. void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
  1885. enum i915_mm_subclass subclass)
  1886. {
  1887. struct sg_table *pages;
  1888. if (i915_gem_object_has_pinned_pages(obj))
  1889. return;
  1890. GEM_BUG_ON(obj->bind_count);
  1891. if (!READ_ONCE(obj->mm.pages))
  1892. return;
  1893. /* May be called by shrinker from within get_pages() (on another bo) */
  1894. mutex_lock_nested(&obj->mm.lock, subclass);
  1895. if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
  1896. goto unlock;
  1897. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1898. * array, hence protect them from being reaped by removing them from gtt
  1899. * lists early. */
  1900. pages = fetch_and_zero(&obj->mm.pages);
  1901. GEM_BUG_ON(!pages);
  1902. if (obj->mm.mapping) {
  1903. void *ptr;
  1904. ptr = page_mask_bits(obj->mm.mapping);
  1905. if (is_vmalloc_addr(ptr))
  1906. vunmap(ptr);
  1907. else
  1908. kunmap(kmap_to_page(ptr));
  1909. obj->mm.mapping = NULL;
  1910. }
  1911. __i915_gem_object_reset_page_iter(obj);
  1912. if (!IS_ERR(pages))
  1913. obj->ops->put_pages(obj, pages);
  1914. unlock:
  1915. mutex_unlock(&obj->mm.lock);
  1916. }
  1917. static bool i915_sg_trim(struct sg_table *orig_st)
  1918. {
  1919. struct sg_table new_st;
  1920. struct scatterlist *sg, *new_sg;
  1921. unsigned int i;
  1922. if (orig_st->nents == orig_st->orig_nents)
  1923. return false;
  1924. if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
  1925. return false;
  1926. new_sg = new_st.sgl;
  1927. for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
  1928. sg_set_page(new_sg, sg_page(sg), sg->length, 0);
  1929. /* called before being DMA mapped, no need to copy sg->dma_* */
  1930. new_sg = sg_next(new_sg);
  1931. }
  1932. GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
  1933. sg_free_table(orig_st);
  1934. *orig_st = new_st;
  1935. return true;
  1936. }
  1937. static struct sg_table *
  1938. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1939. {
  1940. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1941. const unsigned long page_count = obj->base.size / PAGE_SIZE;
  1942. unsigned long i;
  1943. struct address_space *mapping;
  1944. struct sg_table *st;
  1945. struct scatterlist *sg;
  1946. struct sgt_iter sgt_iter;
  1947. struct page *page;
  1948. unsigned long last_pfn = 0; /* suppress gcc warning */
  1949. unsigned int max_segment;
  1950. gfp_t noreclaim;
  1951. int ret;
  1952. /* Assert that the object is not currently in any GPU domain. As it
  1953. * wasn't in the GTT, there shouldn't be any way it could have been in
  1954. * a GPU cache
  1955. */
  1956. GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1957. GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1958. max_segment = swiotlb_max_segment();
  1959. if (!max_segment)
  1960. max_segment = rounddown(UINT_MAX, PAGE_SIZE);
  1961. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1962. if (st == NULL)
  1963. return ERR_PTR(-ENOMEM);
  1964. rebuild_st:
  1965. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1966. kfree(st);
  1967. return ERR_PTR(-ENOMEM);
  1968. }
  1969. /* Get the list of pages out of our struct file. They'll be pinned
  1970. * at this point until we release them.
  1971. *
  1972. * Fail silently without starting the shrinker
  1973. */
  1974. mapping = obj->base.filp->f_mapping;
  1975. noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
  1976. noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
  1977. sg = st->sgl;
  1978. st->nents = 0;
  1979. for (i = 0; i < page_count; i++) {
  1980. const unsigned int shrink[] = {
  1981. I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
  1982. 0,
  1983. }, *s = shrink;
  1984. gfp_t gfp = noreclaim;
  1985. do {
  1986. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1987. if (likely(!IS_ERR(page)))
  1988. break;
  1989. if (!*s) {
  1990. ret = PTR_ERR(page);
  1991. goto err_sg;
  1992. }
  1993. i915_gem_shrink(dev_priv, 2 * page_count, *s++);
  1994. cond_resched();
  1995. /* We've tried hard to allocate the memory by reaping
  1996. * our own buffer, now let the real VM do its job and
  1997. * go down in flames if truly OOM.
  1998. *
  1999. * However, since graphics tend to be disposable,
  2000. * defer the oom here by reporting the ENOMEM back
  2001. * to userspace.
  2002. */
  2003. if (!*s) {
  2004. /* reclaim and warn, but no oom */
  2005. gfp = mapping_gfp_mask(mapping);
  2006. /* Our bo are always dirty and so we require
  2007. * kswapd to reclaim our pages (direct reclaim
  2008. * does not effectively begin pageout of our
  2009. * buffers on its own). However, direct reclaim
  2010. * only waits for kswapd when under allocation
  2011. * congestion. So as a result __GFP_RECLAIM is
  2012. * unreliable and fails to actually reclaim our
  2013. * dirty pages -- unless you try over and over
  2014. * again with !__GFP_NORETRY. However, we still
  2015. * want to fail this allocation rather than
  2016. * trigger the out-of-memory killer and for
  2017. * this we want __GFP_RETRY_MAYFAIL.
  2018. */
  2019. gfp |= __GFP_RETRY_MAYFAIL;
  2020. }
  2021. } while (1);
  2022. if (!i ||
  2023. sg->length >= max_segment ||
  2024. page_to_pfn(page) != last_pfn + 1) {
  2025. if (i)
  2026. sg = sg_next(sg);
  2027. st->nents++;
  2028. sg_set_page(sg, page, PAGE_SIZE, 0);
  2029. } else {
  2030. sg->length += PAGE_SIZE;
  2031. }
  2032. last_pfn = page_to_pfn(page);
  2033. /* Check that the i965g/gm workaround works. */
  2034. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  2035. }
  2036. if (sg) /* loop terminated early; short sg table */
  2037. sg_mark_end(sg);
  2038. /* Trim unused sg entries to avoid wasting memory. */
  2039. i915_sg_trim(st);
  2040. ret = i915_gem_gtt_prepare_pages(obj, st);
  2041. if (ret) {
  2042. /* DMA remapping failed? One possible cause is that
  2043. * it could not reserve enough large entries, asking
  2044. * for PAGE_SIZE chunks instead may be helpful.
  2045. */
  2046. if (max_segment > PAGE_SIZE) {
  2047. for_each_sgt_page(page, sgt_iter, st)
  2048. put_page(page);
  2049. sg_free_table(st);
  2050. max_segment = PAGE_SIZE;
  2051. goto rebuild_st;
  2052. } else {
  2053. dev_warn(&dev_priv->drm.pdev->dev,
  2054. "Failed to DMA remap %lu pages\n",
  2055. page_count);
  2056. goto err_pages;
  2057. }
  2058. }
  2059. if (i915_gem_object_needs_bit17_swizzle(obj))
  2060. i915_gem_object_do_bit_17_swizzle(obj, st);
  2061. return st;
  2062. err_sg:
  2063. sg_mark_end(sg);
  2064. err_pages:
  2065. for_each_sgt_page(page, sgt_iter, st)
  2066. put_page(page);
  2067. sg_free_table(st);
  2068. kfree(st);
  2069. /* shmemfs first checks if there is enough memory to allocate the page
  2070. * and reports ENOSPC should there be insufficient, along with the usual
  2071. * ENOMEM for a genuine allocation failure.
  2072. *
  2073. * We use ENOSPC in our driver to mean that we have run out of aperture
  2074. * space and so want to translate the error from shmemfs back to our
  2075. * usual understanding of ENOMEM.
  2076. */
  2077. if (ret == -ENOSPC)
  2078. ret = -ENOMEM;
  2079. return ERR_PTR(ret);
  2080. }
  2081. void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
  2082. struct sg_table *pages)
  2083. {
  2084. lockdep_assert_held(&obj->mm.lock);
  2085. obj->mm.get_page.sg_pos = pages->sgl;
  2086. obj->mm.get_page.sg_idx = 0;
  2087. obj->mm.pages = pages;
  2088. if (i915_gem_object_is_tiled(obj) &&
  2089. to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  2090. GEM_BUG_ON(obj->mm.quirked);
  2091. __i915_gem_object_pin_pages(obj);
  2092. obj->mm.quirked = true;
  2093. }
  2094. }
  2095. static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2096. {
  2097. struct sg_table *pages;
  2098. GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
  2099. if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
  2100. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  2101. return -EFAULT;
  2102. }
  2103. pages = obj->ops->get_pages(obj);
  2104. if (unlikely(IS_ERR(pages)))
  2105. return PTR_ERR(pages);
  2106. __i915_gem_object_set_pages(obj, pages);
  2107. return 0;
  2108. }
  2109. /* Ensure that the associated pages are gathered from the backing storage
  2110. * and pinned into our object. i915_gem_object_pin_pages() may be called
  2111. * multiple times before they are released by a single call to
  2112. * i915_gem_object_unpin_pages() - once the pages are no longer referenced
  2113. * either as a result of memory pressure (reaping pages under the shrinker)
  2114. * or as the object is itself released.
  2115. */
  2116. int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2117. {
  2118. int err;
  2119. err = mutex_lock_interruptible(&obj->mm.lock);
  2120. if (err)
  2121. return err;
  2122. if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
  2123. err = ____i915_gem_object_get_pages(obj);
  2124. if (err)
  2125. goto unlock;
  2126. smp_mb__before_atomic();
  2127. }
  2128. atomic_inc(&obj->mm.pages_pin_count);
  2129. unlock:
  2130. mutex_unlock(&obj->mm.lock);
  2131. return err;
  2132. }
  2133. /* The 'mapping' part of i915_gem_object_pin_map() below */
  2134. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
  2135. enum i915_map_type type)
  2136. {
  2137. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  2138. struct sg_table *sgt = obj->mm.pages;
  2139. struct sgt_iter sgt_iter;
  2140. struct page *page;
  2141. struct page *stack_pages[32];
  2142. struct page **pages = stack_pages;
  2143. unsigned long i = 0;
  2144. pgprot_t pgprot;
  2145. void *addr;
  2146. /* A single page can always be kmapped */
  2147. if (n_pages == 1 && type == I915_MAP_WB)
  2148. return kmap(sg_page(sgt->sgl));
  2149. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2150. /* Too big for stack -- allocate temporary array instead */
  2151. pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_TEMPORARY);
  2152. if (!pages)
  2153. return NULL;
  2154. }
  2155. for_each_sgt_page(page, sgt_iter, sgt)
  2156. pages[i++] = page;
  2157. /* Check that we have the expected number of pages */
  2158. GEM_BUG_ON(i != n_pages);
  2159. switch (type) {
  2160. case I915_MAP_WB:
  2161. pgprot = PAGE_KERNEL;
  2162. break;
  2163. case I915_MAP_WC:
  2164. pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
  2165. break;
  2166. }
  2167. addr = vmap(pages, n_pages, 0, pgprot);
  2168. if (pages != stack_pages)
  2169. kvfree(pages);
  2170. return addr;
  2171. }
  2172. /* get, pin, and map the pages of the object into kernel space */
  2173. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
  2174. enum i915_map_type type)
  2175. {
  2176. enum i915_map_type has_type;
  2177. bool pinned;
  2178. void *ptr;
  2179. int ret;
  2180. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  2181. ret = mutex_lock_interruptible(&obj->mm.lock);
  2182. if (ret)
  2183. return ERR_PTR(ret);
  2184. pinned = true;
  2185. if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
  2186. if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
  2187. ret = ____i915_gem_object_get_pages(obj);
  2188. if (ret)
  2189. goto err_unlock;
  2190. smp_mb__before_atomic();
  2191. }
  2192. atomic_inc(&obj->mm.pages_pin_count);
  2193. pinned = false;
  2194. }
  2195. GEM_BUG_ON(!obj->mm.pages);
  2196. ptr = page_unpack_bits(obj->mm.mapping, &has_type);
  2197. if (ptr && has_type != type) {
  2198. if (pinned) {
  2199. ret = -EBUSY;
  2200. goto err_unpin;
  2201. }
  2202. if (is_vmalloc_addr(ptr))
  2203. vunmap(ptr);
  2204. else
  2205. kunmap(kmap_to_page(ptr));
  2206. ptr = obj->mm.mapping = NULL;
  2207. }
  2208. if (!ptr) {
  2209. ptr = i915_gem_object_map(obj, type);
  2210. if (!ptr) {
  2211. ret = -ENOMEM;
  2212. goto err_unpin;
  2213. }
  2214. obj->mm.mapping = page_pack_bits(ptr, type);
  2215. }
  2216. out_unlock:
  2217. mutex_unlock(&obj->mm.lock);
  2218. return ptr;
  2219. err_unpin:
  2220. atomic_dec(&obj->mm.pages_pin_count);
  2221. err_unlock:
  2222. ptr = ERR_PTR(ret);
  2223. goto out_unlock;
  2224. }
  2225. static int
  2226. i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
  2227. const struct drm_i915_gem_pwrite *arg)
  2228. {
  2229. struct address_space *mapping = obj->base.filp->f_mapping;
  2230. char __user *user_data = u64_to_user_ptr(arg->data_ptr);
  2231. u64 remain, offset;
  2232. unsigned int pg;
  2233. /* Before we instantiate/pin the backing store for our use, we
  2234. * can prepopulate the shmemfs filp efficiently using a write into
  2235. * the pagecache. We avoid the penalty of instantiating all the
  2236. * pages, important if the user is just writing to a few and never
  2237. * uses the object on the GPU, and using a direct write into shmemfs
  2238. * allows it to avoid the cost of retrieving a page (either swapin
  2239. * or clearing-before-use) before it is overwritten.
  2240. */
  2241. if (READ_ONCE(obj->mm.pages))
  2242. return -ENODEV;
  2243. /* Before the pages are instantiated the object is treated as being
  2244. * in the CPU domain. The pages will be clflushed as required before
  2245. * use, and we can freely write into the pages directly. If userspace
  2246. * races pwrite with any other operation; corruption will ensue -
  2247. * that is userspace's prerogative!
  2248. */
  2249. remain = arg->size;
  2250. offset = arg->offset;
  2251. pg = offset_in_page(offset);
  2252. do {
  2253. unsigned int len, unwritten;
  2254. struct page *page;
  2255. void *data, *vaddr;
  2256. int err;
  2257. len = PAGE_SIZE - pg;
  2258. if (len > remain)
  2259. len = remain;
  2260. err = pagecache_write_begin(obj->base.filp, mapping,
  2261. offset, len, 0,
  2262. &page, &data);
  2263. if (err < 0)
  2264. return err;
  2265. vaddr = kmap(page);
  2266. unwritten = copy_from_user(vaddr + pg, user_data, len);
  2267. kunmap(page);
  2268. err = pagecache_write_end(obj->base.filp, mapping,
  2269. offset, len, len - unwritten,
  2270. page, data);
  2271. if (err < 0)
  2272. return err;
  2273. if (unwritten)
  2274. return -EFAULT;
  2275. remain -= len;
  2276. user_data += len;
  2277. offset += len;
  2278. pg = 0;
  2279. } while (remain);
  2280. return 0;
  2281. }
  2282. static bool ban_context(const struct i915_gem_context *ctx,
  2283. unsigned int score)
  2284. {
  2285. return (i915_gem_context_is_bannable(ctx) &&
  2286. score >= CONTEXT_SCORE_BAN_THRESHOLD);
  2287. }
  2288. static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
  2289. {
  2290. unsigned int score;
  2291. bool banned;
  2292. atomic_inc(&ctx->guilty_count);
  2293. score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
  2294. banned = ban_context(ctx, score);
  2295. DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
  2296. ctx->name, score, yesno(banned));
  2297. if (!banned)
  2298. return;
  2299. i915_gem_context_set_banned(ctx);
  2300. if (!IS_ERR_OR_NULL(ctx->file_priv)) {
  2301. atomic_inc(&ctx->file_priv->context_bans);
  2302. DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
  2303. ctx->name, atomic_read(&ctx->file_priv->context_bans));
  2304. }
  2305. }
  2306. static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
  2307. {
  2308. atomic_inc(&ctx->active_count);
  2309. }
  2310. struct drm_i915_gem_request *
  2311. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2312. {
  2313. struct drm_i915_gem_request *request, *active = NULL;
  2314. unsigned long flags;
  2315. /* We are called by the error capture and reset at a random
  2316. * point in time. In particular, note that neither is crucially
  2317. * ordered with an interrupt. After a hang, the GPU is dead and we
  2318. * assume that no more writes can happen (we waited long enough for
  2319. * all writes that were in transaction to be flushed) - adding an
  2320. * extra delay for a recent interrupt is pointless. Hence, we do
  2321. * not need an engine->irq_seqno_barrier() before the seqno reads.
  2322. */
  2323. spin_lock_irqsave(&engine->timeline->lock, flags);
  2324. list_for_each_entry(request, &engine->timeline->requests, link) {
  2325. if (__i915_gem_request_completed(request,
  2326. request->global_seqno))
  2327. continue;
  2328. GEM_BUG_ON(request->engine != engine);
  2329. GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  2330. &request->fence.flags));
  2331. active = request;
  2332. break;
  2333. }
  2334. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2335. return active;
  2336. }
  2337. static bool engine_stalled(struct intel_engine_cs *engine)
  2338. {
  2339. if (!engine->hangcheck.stalled)
  2340. return false;
  2341. /* Check for possible seqno movement after hang declaration */
  2342. if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
  2343. DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
  2344. return false;
  2345. }
  2346. return true;
  2347. }
  2348. /*
  2349. * Ensure irq handler finishes, and not run again.
  2350. * Also return the active request so that we only search for it once.
  2351. */
  2352. struct drm_i915_gem_request *
  2353. i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
  2354. {
  2355. struct drm_i915_gem_request *request = NULL;
  2356. /* Prevent the signaler thread from updating the request
  2357. * state (by calling dma_fence_signal) as we are processing
  2358. * the reset. The write from the GPU of the seqno is
  2359. * asynchronous and the signaler thread may see a different
  2360. * value to us and declare the request complete, even though
  2361. * the reset routine have picked that request as the active
  2362. * (incomplete) request. This conflict is not handled
  2363. * gracefully!
  2364. */
  2365. kthread_park(engine->breadcrumbs.signaler);
  2366. /* Prevent request submission to the hardware until we have
  2367. * completed the reset in i915_gem_reset_finish(). If a request
  2368. * is completed by one engine, it may then queue a request
  2369. * to a second via its engine->irq_tasklet *just* as we are
  2370. * calling engine->init_hw() and also writing the ELSP.
  2371. * Turning off the engine->irq_tasklet until the reset is over
  2372. * prevents the race.
  2373. */
  2374. tasklet_kill(&engine->irq_tasklet);
  2375. tasklet_disable(&engine->irq_tasklet);
  2376. if (engine->irq_seqno_barrier)
  2377. engine->irq_seqno_barrier(engine);
  2378. request = i915_gem_find_active_request(engine);
  2379. if (request && request->fence.error == -EIO)
  2380. request = ERR_PTR(-EIO); /* Previous reset failed! */
  2381. return request;
  2382. }
  2383. int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
  2384. {
  2385. struct intel_engine_cs *engine;
  2386. struct drm_i915_gem_request *request;
  2387. enum intel_engine_id id;
  2388. int err = 0;
  2389. for_each_engine(engine, dev_priv, id) {
  2390. request = i915_gem_reset_prepare_engine(engine);
  2391. if (IS_ERR(request)) {
  2392. err = PTR_ERR(request);
  2393. continue;
  2394. }
  2395. engine->hangcheck.active_request = request;
  2396. }
  2397. i915_gem_revoke_fences(dev_priv);
  2398. return err;
  2399. }
  2400. static void skip_request(struct drm_i915_gem_request *request)
  2401. {
  2402. void *vaddr = request->ring->vaddr;
  2403. u32 head;
  2404. /* As this request likely depends on state from the lost
  2405. * context, clear out all the user operations leaving the
  2406. * breadcrumb at the end (so we get the fence notifications).
  2407. */
  2408. head = request->head;
  2409. if (request->postfix < head) {
  2410. memset(vaddr + head, 0, request->ring->size - head);
  2411. head = 0;
  2412. }
  2413. memset(vaddr + head, 0, request->postfix - head);
  2414. dma_fence_set_error(&request->fence, -EIO);
  2415. }
  2416. static void engine_skip_context(struct drm_i915_gem_request *request)
  2417. {
  2418. struct intel_engine_cs *engine = request->engine;
  2419. struct i915_gem_context *hung_ctx = request->ctx;
  2420. struct intel_timeline *timeline;
  2421. unsigned long flags;
  2422. timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
  2423. spin_lock_irqsave(&engine->timeline->lock, flags);
  2424. spin_lock(&timeline->lock);
  2425. list_for_each_entry_continue(request, &engine->timeline->requests, link)
  2426. if (request->ctx == hung_ctx)
  2427. skip_request(request);
  2428. list_for_each_entry(request, &timeline->requests, link)
  2429. skip_request(request);
  2430. spin_unlock(&timeline->lock);
  2431. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2432. }
  2433. /* Returns the request if it was guilty of the hang */
  2434. static struct drm_i915_gem_request *
  2435. i915_gem_reset_request(struct intel_engine_cs *engine,
  2436. struct drm_i915_gem_request *request)
  2437. {
  2438. /* The guilty request will get skipped on a hung engine.
  2439. *
  2440. * Users of client default contexts do not rely on logical
  2441. * state preserved between batches so it is safe to execute
  2442. * queued requests following the hang. Non default contexts
  2443. * rely on preserved state, so skipping a batch loses the
  2444. * evolution of the state and it needs to be considered corrupted.
  2445. * Executing more queued batches on top of corrupted state is
  2446. * risky. But we take the risk by trying to advance through
  2447. * the queued requests in order to make the client behaviour
  2448. * more predictable around resets, by not throwing away random
  2449. * amount of batches it has prepared for execution. Sophisticated
  2450. * clients can use gem_reset_stats_ioctl and dma fence status
  2451. * (exported via sync_file info ioctl on explicit fences) to observe
  2452. * when it loses the context state and should rebuild accordingly.
  2453. *
  2454. * The context ban, and ultimately the client ban, mechanism are safety
  2455. * valves if client submission ends up resulting in nothing more than
  2456. * subsequent hangs.
  2457. */
  2458. if (engine_stalled(engine)) {
  2459. i915_gem_context_mark_guilty(request->ctx);
  2460. skip_request(request);
  2461. /* If this context is now banned, skip all pending requests. */
  2462. if (i915_gem_context_is_banned(request->ctx))
  2463. engine_skip_context(request);
  2464. } else {
  2465. /*
  2466. * Since this is not the hung engine, it may have advanced
  2467. * since the hang declaration. Double check by refinding
  2468. * the active request at the time of the reset.
  2469. */
  2470. request = i915_gem_find_active_request(engine);
  2471. if (request) {
  2472. i915_gem_context_mark_innocent(request->ctx);
  2473. dma_fence_set_error(&request->fence, -EAGAIN);
  2474. /* Rewind the engine to replay the incomplete rq */
  2475. spin_lock_irq(&engine->timeline->lock);
  2476. request = list_prev_entry(request, link);
  2477. if (&request->link == &engine->timeline->requests)
  2478. request = NULL;
  2479. spin_unlock_irq(&engine->timeline->lock);
  2480. }
  2481. }
  2482. return request;
  2483. }
  2484. void i915_gem_reset_engine(struct intel_engine_cs *engine,
  2485. struct drm_i915_gem_request *request)
  2486. {
  2487. engine->irq_posted = 0;
  2488. if (request)
  2489. request = i915_gem_reset_request(engine, request);
  2490. if (request) {
  2491. DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
  2492. engine->name, request->global_seqno);
  2493. }
  2494. /* Setup the CS to resume from the breadcrumb of the hung request */
  2495. engine->reset_hw(engine, request);
  2496. }
  2497. void i915_gem_reset(struct drm_i915_private *dev_priv)
  2498. {
  2499. struct intel_engine_cs *engine;
  2500. enum intel_engine_id id;
  2501. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2502. i915_gem_retire_requests(dev_priv);
  2503. for_each_engine(engine, dev_priv, id) {
  2504. struct i915_gem_context *ctx;
  2505. i915_gem_reset_engine(engine, engine->hangcheck.active_request);
  2506. ctx = fetch_and_zero(&engine->last_retired_context);
  2507. if (ctx)
  2508. engine->context_unpin(engine, ctx);
  2509. }
  2510. i915_gem_restore_fences(dev_priv);
  2511. if (dev_priv->gt.awake) {
  2512. intel_sanitize_gt_powersave(dev_priv);
  2513. intel_enable_gt_powersave(dev_priv);
  2514. if (INTEL_GEN(dev_priv) >= 6)
  2515. gen6_rps_busy(dev_priv);
  2516. }
  2517. }
  2518. void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
  2519. {
  2520. tasklet_enable(&engine->irq_tasklet);
  2521. kthread_unpark(engine->breadcrumbs.signaler);
  2522. }
  2523. void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
  2524. {
  2525. struct intel_engine_cs *engine;
  2526. enum intel_engine_id id;
  2527. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2528. for_each_engine(engine, dev_priv, id) {
  2529. engine->hangcheck.active_request = NULL;
  2530. i915_gem_reset_finish_engine(engine);
  2531. }
  2532. }
  2533. static void nop_submit_request(struct drm_i915_gem_request *request)
  2534. {
  2535. GEM_BUG_ON(!i915_terminally_wedged(&request->i915->gpu_error));
  2536. dma_fence_set_error(&request->fence, -EIO);
  2537. i915_gem_request_submit(request);
  2538. intel_engine_init_global_seqno(request->engine, request->global_seqno);
  2539. }
  2540. static void engine_set_wedged(struct intel_engine_cs *engine)
  2541. {
  2542. struct drm_i915_gem_request *request;
  2543. unsigned long flags;
  2544. /* We need to be sure that no thread is running the old callback as
  2545. * we install the nop handler (otherwise we would submit a request
  2546. * to hardware that will never complete). In order to prevent this
  2547. * race, we wait until the machine is idle before making the swap
  2548. * (using stop_machine()).
  2549. */
  2550. engine->submit_request = nop_submit_request;
  2551. /* Mark all executing requests as skipped */
  2552. spin_lock_irqsave(&engine->timeline->lock, flags);
  2553. list_for_each_entry(request, &engine->timeline->requests, link)
  2554. if (!i915_gem_request_completed(request))
  2555. dma_fence_set_error(&request->fence, -EIO);
  2556. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2557. /*
  2558. * Clear the execlists queue up before freeing the requests, as those
  2559. * are the ones that keep the context and ringbuffer backing objects
  2560. * pinned in place.
  2561. */
  2562. if (i915.enable_execlists) {
  2563. struct execlist_port *port = engine->execlist_port;
  2564. unsigned long flags;
  2565. unsigned int n;
  2566. spin_lock_irqsave(&engine->timeline->lock, flags);
  2567. for (n = 0; n < ARRAY_SIZE(engine->execlist_port); n++)
  2568. i915_gem_request_put(port_request(&port[n]));
  2569. memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
  2570. engine->execlist_queue = RB_ROOT;
  2571. engine->execlist_first = NULL;
  2572. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2573. /* The port is checked prior to scheduling a tasklet, but
  2574. * just in case we have suspended the tasklet to do the
  2575. * wedging make sure that when it wakes, it decides there
  2576. * is no work to do by clearing the irq_posted bit.
  2577. */
  2578. clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
  2579. }
  2580. /* Mark all pending requests as complete so that any concurrent
  2581. * (lockless) lookup doesn't try and wait upon the request as we
  2582. * reset it.
  2583. */
  2584. intel_engine_init_global_seqno(engine,
  2585. intel_engine_last_submit(engine));
  2586. }
  2587. static int __i915_gem_set_wedged_BKL(void *data)
  2588. {
  2589. struct drm_i915_private *i915 = data;
  2590. struct intel_engine_cs *engine;
  2591. enum intel_engine_id id;
  2592. for_each_engine(engine, i915, id)
  2593. engine_set_wedged(engine);
  2594. set_bit(I915_WEDGED, &i915->gpu_error.flags);
  2595. wake_up_all(&i915->gpu_error.reset_queue);
  2596. return 0;
  2597. }
  2598. void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
  2599. {
  2600. stop_machine(__i915_gem_set_wedged_BKL, dev_priv, NULL);
  2601. }
  2602. bool i915_gem_unset_wedged(struct drm_i915_private *i915)
  2603. {
  2604. struct i915_gem_timeline *tl;
  2605. int i;
  2606. lockdep_assert_held(&i915->drm.struct_mutex);
  2607. if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
  2608. return true;
  2609. /* Before unwedging, make sure that all pending operations
  2610. * are flushed and errored out - we may have requests waiting upon
  2611. * third party fences. We marked all inflight requests as EIO, and
  2612. * every execbuf since returned EIO, for consistency we want all
  2613. * the currently pending requests to also be marked as EIO, which
  2614. * is done inside our nop_submit_request - and so we must wait.
  2615. *
  2616. * No more can be submitted until we reset the wedged bit.
  2617. */
  2618. list_for_each_entry(tl, &i915->gt.timelines, link) {
  2619. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2620. struct drm_i915_gem_request *rq;
  2621. rq = i915_gem_active_peek(&tl->engine[i].last_request,
  2622. &i915->drm.struct_mutex);
  2623. if (!rq)
  2624. continue;
  2625. /* We can't use our normal waiter as we want to
  2626. * avoid recursively trying to handle the current
  2627. * reset. The basic dma_fence_default_wait() installs
  2628. * a callback for dma_fence_signal(), which is
  2629. * triggered by our nop handler (indirectly, the
  2630. * callback enables the signaler thread which is
  2631. * woken by the nop_submit_request() advancing the seqno
  2632. * and when the seqno passes the fence, the signaler
  2633. * then signals the fence waking us up).
  2634. */
  2635. if (dma_fence_default_wait(&rq->fence, true,
  2636. MAX_SCHEDULE_TIMEOUT) < 0)
  2637. return false;
  2638. }
  2639. }
  2640. /* Undo nop_submit_request. We prevent all new i915 requests from
  2641. * being queued (by disallowing execbuf whilst wedged) so having
  2642. * waited for all active requests above, we know the system is idle
  2643. * and do not have to worry about a thread being inside
  2644. * engine->submit_request() as we swap over. So unlike installing
  2645. * the nop_submit_request on reset, we can do this from normal
  2646. * context and do not require stop_machine().
  2647. */
  2648. intel_engines_reset_default_submission(i915);
  2649. i915_gem_contexts_lost(i915);
  2650. smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
  2651. clear_bit(I915_WEDGED, &i915->gpu_error.flags);
  2652. return true;
  2653. }
  2654. static void
  2655. i915_gem_retire_work_handler(struct work_struct *work)
  2656. {
  2657. struct drm_i915_private *dev_priv =
  2658. container_of(work, typeof(*dev_priv), gt.retire_work.work);
  2659. struct drm_device *dev = &dev_priv->drm;
  2660. /* Come back later if the device is busy... */
  2661. if (mutex_trylock(&dev->struct_mutex)) {
  2662. i915_gem_retire_requests(dev_priv);
  2663. mutex_unlock(&dev->struct_mutex);
  2664. }
  2665. /* Keep the retire handler running until we are finally idle.
  2666. * We do not need to do this test under locking as in the worst-case
  2667. * we queue the retire worker once too often.
  2668. */
  2669. if (READ_ONCE(dev_priv->gt.awake)) {
  2670. i915_queue_hangcheck(dev_priv);
  2671. queue_delayed_work(dev_priv->wq,
  2672. &dev_priv->gt.retire_work,
  2673. round_jiffies_up_relative(HZ));
  2674. }
  2675. }
  2676. static void
  2677. i915_gem_idle_work_handler(struct work_struct *work)
  2678. {
  2679. struct drm_i915_private *dev_priv =
  2680. container_of(work, typeof(*dev_priv), gt.idle_work.work);
  2681. struct drm_device *dev = &dev_priv->drm;
  2682. bool rearm_hangcheck;
  2683. if (!READ_ONCE(dev_priv->gt.awake))
  2684. return;
  2685. /*
  2686. * Wait for last execlists context complete, but bail out in case a
  2687. * new request is submitted.
  2688. */
  2689. wait_for(intel_engines_are_idle(dev_priv), 10);
  2690. if (READ_ONCE(dev_priv->gt.active_requests))
  2691. return;
  2692. rearm_hangcheck =
  2693. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  2694. if (!mutex_trylock(&dev->struct_mutex)) {
  2695. /* Currently busy, come back later */
  2696. mod_delayed_work(dev_priv->wq,
  2697. &dev_priv->gt.idle_work,
  2698. msecs_to_jiffies(50));
  2699. goto out_rearm;
  2700. }
  2701. /*
  2702. * New request retired after this work handler started, extend active
  2703. * period until next instance of the work.
  2704. */
  2705. if (work_pending(work))
  2706. goto out_unlock;
  2707. if (dev_priv->gt.active_requests)
  2708. goto out_unlock;
  2709. if (wait_for(intel_engines_are_idle(dev_priv), 10))
  2710. DRM_ERROR("Timeout waiting for engines to idle\n");
  2711. intel_engines_mark_idle(dev_priv);
  2712. i915_gem_timelines_mark_idle(dev_priv);
  2713. GEM_BUG_ON(!dev_priv->gt.awake);
  2714. dev_priv->gt.awake = false;
  2715. rearm_hangcheck = false;
  2716. if (INTEL_GEN(dev_priv) >= 6)
  2717. gen6_rps_idle(dev_priv);
  2718. intel_runtime_pm_put(dev_priv);
  2719. out_unlock:
  2720. mutex_unlock(&dev->struct_mutex);
  2721. out_rearm:
  2722. if (rearm_hangcheck) {
  2723. GEM_BUG_ON(!dev_priv->gt.awake);
  2724. i915_queue_hangcheck(dev_priv);
  2725. }
  2726. }
  2727. void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
  2728. {
  2729. struct drm_i915_gem_object *obj = to_intel_bo(gem);
  2730. struct drm_i915_file_private *fpriv = file->driver_priv;
  2731. struct i915_vma *vma, *vn;
  2732. mutex_lock(&obj->base.dev->struct_mutex);
  2733. list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
  2734. if (vma->vm->file == fpriv)
  2735. i915_vma_close(vma);
  2736. vma = obj->vma_hashed;
  2737. if (vma && vma->ctx->file_priv == fpriv)
  2738. i915_vma_unlink_ctx(vma);
  2739. if (i915_gem_object_is_active(obj) &&
  2740. !i915_gem_object_has_active_reference(obj)) {
  2741. i915_gem_object_set_active_reference(obj);
  2742. i915_gem_object_get(obj);
  2743. }
  2744. mutex_unlock(&obj->base.dev->struct_mutex);
  2745. }
  2746. static unsigned long to_wait_timeout(s64 timeout_ns)
  2747. {
  2748. if (timeout_ns < 0)
  2749. return MAX_SCHEDULE_TIMEOUT;
  2750. if (timeout_ns == 0)
  2751. return 0;
  2752. return nsecs_to_jiffies_timeout(timeout_ns);
  2753. }
  2754. /**
  2755. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2756. * @dev: drm device pointer
  2757. * @data: ioctl data blob
  2758. * @file: drm file pointer
  2759. *
  2760. * Returns 0 if successful, else an error is returned with the remaining time in
  2761. * the timeout parameter.
  2762. * -ETIME: object is still busy after timeout
  2763. * -ERESTARTSYS: signal interrupted the wait
  2764. * -ENONENT: object doesn't exist
  2765. * Also possible, but rare:
  2766. * -EAGAIN: GPU wedged
  2767. * -ENOMEM: damn
  2768. * -ENODEV: Internal IRQ fail
  2769. * -E?: The add request failed
  2770. *
  2771. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2772. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2773. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2774. * without holding struct_mutex the object may become re-busied before this
  2775. * function completes. A similar but shorter * race condition exists in the busy
  2776. * ioctl
  2777. */
  2778. int
  2779. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2780. {
  2781. struct drm_i915_gem_wait *args = data;
  2782. struct drm_i915_gem_object *obj;
  2783. ktime_t start;
  2784. long ret;
  2785. if (args->flags != 0)
  2786. return -EINVAL;
  2787. obj = i915_gem_object_lookup(file, args->bo_handle);
  2788. if (!obj)
  2789. return -ENOENT;
  2790. start = ktime_get();
  2791. ret = i915_gem_object_wait(obj,
  2792. I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
  2793. to_wait_timeout(args->timeout_ns),
  2794. to_rps_client(file));
  2795. if (args->timeout_ns > 0) {
  2796. args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
  2797. if (args->timeout_ns < 0)
  2798. args->timeout_ns = 0;
  2799. /*
  2800. * Apparently ktime isn't accurate enough and occasionally has a
  2801. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  2802. * things up to make the test happy. We allow up to 1 jiffy.
  2803. *
  2804. * This is a regression from the timespec->ktime conversion.
  2805. */
  2806. if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
  2807. args->timeout_ns = 0;
  2808. }
  2809. i915_gem_object_put(obj);
  2810. return ret;
  2811. }
  2812. static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
  2813. {
  2814. int ret, i;
  2815. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2816. ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
  2817. if (ret)
  2818. return ret;
  2819. }
  2820. return 0;
  2821. }
  2822. static int wait_for_engine(struct intel_engine_cs *engine, int timeout_ms)
  2823. {
  2824. return wait_for(intel_engine_is_idle(engine), timeout_ms);
  2825. }
  2826. static int wait_for_engines(struct drm_i915_private *i915)
  2827. {
  2828. struct intel_engine_cs *engine;
  2829. enum intel_engine_id id;
  2830. for_each_engine(engine, i915, id) {
  2831. if (GEM_WARN_ON(wait_for_engine(engine, 50))) {
  2832. i915_gem_set_wedged(i915);
  2833. return -EIO;
  2834. }
  2835. GEM_BUG_ON(intel_engine_get_seqno(engine) !=
  2836. intel_engine_last_submit(engine));
  2837. }
  2838. return 0;
  2839. }
  2840. int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
  2841. {
  2842. int ret;
  2843. /* If the device is asleep, we have no requests outstanding */
  2844. if (!READ_ONCE(i915->gt.awake))
  2845. return 0;
  2846. if (flags & I915_WAIT_LOCKED) {
  2847. struct i915_gem_timeline *tl;
  2848. lockdep_assert_held(&i915->drm.struct_mutex);
  2849. list_for_each_entry(tl, &i915->gt.timelines, link) {
  2850. ret = wait_for_timeline(tl, flags);
  2851. if (ret)
  2852. return ret;
  2853. }
  2854. i915_gem_retire_requests(i915);
  2855. GEM_BUG_ON(i915->gt.active_requests);
  2856. ret = wait_for_engines(i915);
  2857. } else {
  2858. ret = wait_for_timeline(&i915->gt.global_timeline, flags);
  2859. }
  2860. return ret;
  2861. }
  2862. static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
  2863. {
  2864. /*
  2865. * We manually flush the CPU domain so that we can override and
  2866. * force the flush for the display, and perform it asyncrhonously.
  2867. */
  2868. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  2869. if (obj->cache_dirty)
  2870. i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
  2871. obj->base.write_domain = 0;
  2872. }
  2873. void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
  2874. {
  2875. if (!READ_ONCE(obj->pin_display))
  2876. return;
  2877. mutex_lock(&obj->base.dev->struct_mutex);
  2878. __i915_gem_object_flush_for_display(obj);
  2879. mutex_unlock(&obj->base.dev->struct_mutex);
  2880. }
  2881. /**
  2882. * Moves a single object to the WC read, and possibly write domain.
  2883. * @obj: object to act on
  2884. * @write: ask for write access or read only
  2885. *
  2886. * This function returns when the move is complete, including waiting on
  2887. * flushes to occur.
  2888. */
  2889. int
  2890. i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
  2891. {
  2892. int ret;
  2893. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2894. ret = i915_gem_object_wait(obj,
  2895. I915_WAIT_INTERRUPTIBLE |
  2896. I915_WAIT_LOCKED |
  2897. (write ? I915_WAIT_ALL : 0),
  2898. MAX_SCHEDULE_TIMEOUT,
  2899. NULL);
  2900. if (ret)
  2901. return ret;
  2902. if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
  2903. return 0;
  2904. /* Flush and acquire obj->pages so that we are coherent through
  2905. * direct access in memory with previous cached writes through
  2906. * shmemfs and that our cache domain tracking remains valid.
  2907. * For example, if the obj->filp was moved to swap without us
  2908. * being notified and releasing the pages, we would mistakenly
  2909. * continue to assume that the obj remained out of the CPU cached
  2910. * domain.
  2911. */
  2912. ret = i915_gem_object_pin_pages(obj);
  2913. if (ret)
  2914. return ret;
  2915. flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
  2916. /* Serialise direct access to this object with the barriers for
  2917. * coherent writes from the GPU, by effectively invalidating the
  2918. * WC domain upon first access.
  2919. */
  2920. if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
  2921. mb();
  2922. /* It should now be out of any other write domains, and we can update
  2923. * the domain values for our changes.
  2924. */
  2925. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
  2926. obj->base.read_domains |= I915_GEM_DOMAIN_WC;
  2927. if (write) {
  2928. obj->base.read_domains = I915_GEM_DOMAIN_WC;
  2929. obj->base.write_domain = I915_GEM_DOMAIN_WC;
  2930. obj->mm.dirty = true;
  2931. }
  2932. i915_gem_object_unpin_pages(obj);
  2933. return 0;
  2934. }
  2935. /**
  2936. * Moves a single object to the GTT read, and possibly write domain.
  2937. * @obj: object to act on
  2938. * @write: ask for write access or read only
  2939. *
  2940. * This function returns when the move is complete, including waiting on
  2941. * flushes to occur.
  2942. */
  2943. int
  2944. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2945. {
  2946. int ret;
  2947. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2948. ret = i915_gem_object_wait(obj,
  2949. I915_WAIT_INTERRUPTIBLE |
  2950. I915_WAIT_LOCKED |
  2951. (write ? I915_WAIT_ALL : 0),
  2952. MAX_SCHEDULE_TIMEOUT,
  2953. NULL);
  2954. if (ret)
  2955. return ret;
  2956. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2957. return 0;
  2958. /* Flush and acquire obj->pages so that we are coherent through
  2959. * direct access in memory with previous cached writes through
  2960. * shmemfs and that our cache domain tracking remains valid.
  2961. * For example, if the obj->filp was moved to swap without us
  2962. * being notified and releasing the pages, we would mistakenly
  2963. * continue to assume that the obj remained out of the CPU cached
  2964. * domain.
  2965. */
  2966. ret = i915_gem_object_pin_pages(obj);
  2967. if (ret)
  2968. return ret;
  2969. flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
  2970. /* Serialise direct access to this object with the barriers for
  2971. * coherent writes from the GPU, by effectively invalidating the
  2972. * GTT domain upon first access.
  2973. */
  2974. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2975. mb();
  2976. /* It should now be out of any other write domains, and we can update
  2977. * the domain values for our changes.
  2978. */
  2979. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2980. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2981. if (write) {
  2982. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2983. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2984. obj->mm.dirty = true;
  2985. }
  2986. i915_gem_object_unpin_pages(obj);
  2987. return 0;
  2988. }
  2989. /**
  2990. * Changes the cache-level of an object across all VMA.
  2991. * @obj: object to act on
  2992. * @cache_level: new cache level to set for the object
  2993. *
  2994. * After this function returns, the object will be in the new cache-level
  2995. * across all GTT and the contents of the backing storage will be coherent,
  2996. * with respect to the new cache-level. In order to keep the backing storage
  2997. * coherent for all users, we only allow a single cache level to be set
  2998. * globally on the object and prevent it from being changed whilst the
  2999. * hardware is reading from the object. That is if the object is currently
  3000. * on the scanout it will be set to uncached (or equivalent display
  3001. * cache coherency) and all non-MOCS GPU access will also be uncached so
  3002. * that all direct access to the scanout remains coherent.
  3003. */
  3004. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3005. enum i915_cache_level cache_level)
  3006. {
  3007. struct i915_vma *vma;
  3008. int ret;
  3009. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3010. if (obj->cache_level == cache_level)
  3011. return 0;
  3012. /* Inspect the list of currently bound VMA and unbind any that would
  3013. * be invalid given the new cache-level. This is principally to
  3014. * catch the issue of the CS prefetch crossing page boundaries and
  3015. * reading an invalid PTE on older architectures.
  3016. */
  3017. restart:
  3018. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3019. if (!drm_mm_node_allocated(&vma->node))
  3020. continue;
  3021. if (i915_vma_is_pinned(vma)) {
  3022. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3023. return -EBUSY;
  3024. }
  3025. if (i915_gem_valid_gtt_space(vma, cache_level))
  3026. continue;
  3027. ret = i915_vma_unbind(vma);
  3028. if (ret)
  3029. return ret;
  3030. /* As unbinding may affect other elements in the
  3031. * obj->vma_list (due to side-effects from retiring
  3032. * an active vma), play safe and restart the iterator.
  3033. */
  3034. goto restart;
  3035. }
  3036. /* We can reuse the existing drm_mm nodes but need to change the
  3037. * cache-level on the PTE. We could simply unbind them all and
  3038. * rebind with the correct cache-level on next use. However since
  3039. * we already have a valid slot, dma mapping, pages etc, we may as
  3040. * rewrite the PTE in the belief that doing so tramples upon less
  3041. * state and so involves less work.
  3042. */
  3043. if (obj->bind_count) {
  3044. /* Before we change the PTE, the GPU must not be accessing it.
  3045. * If we wait upon the object, we know that all the bound
  3046. * VMA are no longer active.
  3047. */
  3048. ret = i915_gem_object_wait(obj,
  3049. I915_WAIT_INTERRUPTIBLE |
  3050. I915_WAIT_LOCKED |
  3051. I915_WAIT_ALL,
  3052. MAX_SCHEDULE_TIMEOUT,
  3053. NULL);
  3054. if (ret)
  3055. return ret;
  3056. if (!HAS_LLC(to_i915(obj->base.dev)) &&
  3057. cache_level != I915_CACHE_NONE) {
  3058. /* Access to snoopable pages through the GTT is
  3059. * incoherent and on some machines causes a hard
  3060. * lockup. Relinquish the CPU mmaping to force
  3061. * userspace to refault in the pages and we can
  3062. * then double check if the GTT mapping is still
  3063. * valid for that pointer access.
  3064. */
  3065. i915_gem_release_mmap(obj);
  3066. /* As we no longer need a fence for GTT access,
  3067. * we can relinquish it now (and so prevent having
  3068. * to steal a fence from someone else on the next
  3069. * fence request). Note GPU activity would have
  3070. * dropped the fence as all snoopable access is
  3071. * supposed to be linear.
  3072. */
  3073. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3074. ret = i915_vma_put_fence(vma);
  3075. if (ret)
  3076. return ret;
  3077. }
  3078. } else {
  3079. /* We either have incoherent backing store and
  3080. * so no GTT access or the architecture is fully
  3081. * coherent. In such cases, existing GTT mmaps
  3082. * ignore the cache bit in the PTE and we can
  3083. * rewrite it without confusing the GPU or having
  3084. * to force userspace to fault back in its mmaps.
  3085. */
  3086. }
  3087. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3088. if (!drm_mm_node_allocated(&vma->node))
  3089. continue;
  3090. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3091. if (ret)
  3092. return ret;
  3093. }
  3094. }
  3095. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3096. vma->node.color = cache_level;
  3097. obj->cache_level = cache_level;
  3098. obj->cache_coherent = i915_gem_object_is_coherent(obj);
  3099. obj->cache_dirty = true; /* Always invalidate stale cachelines */
  3100. return 0;
  3101. }
  3102. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3103. struct drm_file *file)
  3104. {
  3105. struct drm_i915_gem_caching *args = data;
  3106. struct drm_i915_gem_object *obj;
  3107. int err = 0;
  3108. rcu_read_lock();
  3109. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3110. if (!obj) {
  3111. err = -ENOENT;
  3112. goto out;
  3113. }
  3114. switch (obj->cache_level) {
  3115. case I915_CACHE_LLC:
  3116. case I915_CACHE_L3_LLC:
  3117. args->caching = I915_CACHING_CACHED;
  3118. break;
  3119. case I915_CACHE_WT:
  3120. args->caching = I915_CACHING_DISPLAY;
  3121. break;
  3122. default:
  3123. args->caching = I915_CACHING_NONE;
  3124. break;
  3125. }
  3126. out:
  3127. rcu_read_unlock();
  3128. return err;
  3129. }
  3130. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3131. struct drm_file *file)
  3132. {
  3133. struct drm_i915_private *i915 = to_i915(dev);
  3134. struct drm_i915_gem_caching *args = data;
  3135. struct drm_i915_gem_object *obj;
  3136. enum i915_cache_level level;
  3137. int ret = 0;
  3138. switch (args->caching) {
  3139. case I915_CACHING_NONE:
  3140. level = I915_CACHE_NONE;
  3141. break;
  3142. case I915_CACHING_CACHED:
  3143. /*
  3144. * Due to a HW issue on BXT A stepping, GPU stores via a
  3145. * snooped mapping may leave stale data in a corresponding CPU
  3146. * cacheline, whereas normally such cachelines would get
  3147. * invalidated.
  3148. */
  3149. if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
  3150. return -ENODEV;
  3151. level = I915_CACHE_LLC;
  3152. break;
  3153. case I915_CACHING_DISPLAY:
  3154. level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
  3155. break;
  3156. default:
  3157. return -EINVAL;
  3158. }
  3159. obj = i915_gem_object_lookup(file, args->handle);
  3160. if (!obj)
  3161. return -ENOENT;
  3162. if (obj->cache_level == level)
  3163. goto out;
  3164. ret = i915_gem_object_wait(obj,
  3165. I915_WAIT_INTERRUPTIBLE,
  3166. MAX_SCHEDULE_TIMEOUT,
  3167. to_rps_client(file));
  3168. if (ret)
  3169. goto out;
  3170. ret = i915_mutex_lock_interruptible(dev);
  3171. if (ret)
  3172. goto out;
  3173. ret = i915_gem_object_set_cache_level(obj, level);
  3174. mutex_unlock(&dev->struct_mutex);
  3175. out:
  3176. i915_gem_object_put(obj);
  3177. return ret;
  3178. }
  3179. /*
  3180. * Prepare buffer for display plane (scanout, cursors, etc).
  3181. * Can be called from an uninterruptible phase (modesetting) and allows
  3182. * any flushes to be pipelined (for pageflips).
  3183. */
  3184. struct i915_vma *
  3185. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3186. u32 alignment,
  3187. const struct i915_ggtt_view *view)
  3188. {
  3189. struct i915_vma *vma;
  3190. int ret;
  3191. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3192. /* Mark the pin_display early so that we account for the
  3193. * display coherency whilst setting up the cache domains.
  3194. */
  3195. obj->pin_display++;
  3196. /* The display engine is not coherent with the LLC cache on gen6. As
  3197. * a result, we make sure that the pinning that is about to occur is
  3198. * done with uncached PTEs. This is lowest common denominator for all
  3199. * chipsets.
  3200. *
  3201. * However for gen6+, we could do better by using the GFDT bit instead
  3202. * of uncaching, which would allow us to flush all the LLC-cached data
  3203. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3204. */
  3205. ret = i915_gem_object_set_cache_level(obj,
  3206. HAS_WT(to_i915(obj->base.dev)) ?
  3207. I915_CACHE_WT : I915_CACHE_NONE);
  3208. if (ret) {
  3209. vma = ERR_PTR(ret);
  3210. goto err_unpin_display;
  3211. }
  3212. /* As the user may map the buffer once pinned in the display plane
  3213. * (e.g. libkms for the bootup splash), we have to ensure that we
  3214. * always use map_and_fenceable for all scanout buffers. However,
  3215. * it may simply be too big to fit into mappable, in which case
  3216. * put it anyway and hope that userspace can cope (but always first
  3217. * try to preserve the existing ABI).
  3218. */
  3219. vma = ERR_PTR(-ENOSPC);
  3220. if (!view || view->type == I915_GGTT_VIEW_NORMAL)
  3221. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
  3222. PIN_MAPPABLE | PIN_NONBLOCK);
  3223. if (IS_ERR(vma)) {
  3224. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3225. unsigned int flags;
  3226. /* Valleyview is definitely limited to scanning out the first
  3227. * 512MiB. Lets presume this behaviour was inherited from the
  3228. * g4x display engine and that all earlier gen are similarly
  3229. * limited. Testing suggests that it is a little more
  3230. * complicated than this. For example, Cherryview appears quite
  3231. * happy to scanout from anywhere within its global aperture.
  3232. */
  3233. flags = 0;
  3234. if (HAS_GMCH_DISPLAY(i915))
  3235. flags = PIN_MAPPABLE;
  3236. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
  3237. }
  3238. if (IS_ERR(vma))
  3239. goto err_unpin_display;
  3240. vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
  3241. /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
  3242. __i915_gem_object_flush_for_display(obj);
  3243. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  3244. /* It should now be out of any other write domains, and we can update
  3245. * the domain values for our changes.
  3246. */
  3247. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3248. return vma;
  3249. err_unpin_display:
  3250. obj->pin_display--;
  3251. return vma;
  3252. }
  3253. void
  3254. i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
  3255. {
  3256. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  3257. if (WARN_ON(vma->obj->pin_display == 0))
  3258. return;
  3259. if (--vma->obj->pin_display == 0)
  3260. vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
  3261. /* Bump the LRU to try and avoid premature eviction whilst flipping */
  3262. i915_gem_object_bump_inactive_ggtt(vma->obj);
  3263. i915_vma_unpin(vma);
  3264. }
  3265. /**
  3266. * Moves a single object to the CPU read, and possibly write domain.
  3267. * @obj: object to act on
  3268. * @write: requesting write or read-only access
  3269. *
  3270. * This function returns when the move is complete, including waiting on
  3271. * flushes to occur.
  3272. */
  3273. int
  3274. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3275. {
  3276. int ret;
  3277. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3278. ret = i915_gem_object_wait(obj,
  3279. I915_WAIT_INTERRUPTIBLE |
  3280. I915_WAIT_LOCKED |
  3281. (write ? I915_WAIT_ALL : 0),
  3282. MAX_SCHEDULE_TIMEOUT,
  3283. NULL);
  3284. if (ret)
  3285. return ret;
  3286. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  3287. /* Flush the CPU cache if it's still invalid. */
  3288. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3289. i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
  3290. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3291. }
  3292. /* It should now be out of any other write domains, and we can update
  3293. * the domain values for our changes.
  3294. */
  3295. GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  3296. /* If we're writing through the CPU, then the GPU read domains will
  3297. * need to be invalidated at next use.
  3298. */
  3299. if (write)
  3300. __start_cpu_write(obj);
  3301. return 0;
  3302. }
  3303. /* Throttle our rendering by waiting until the ring has completed our requests
  3304. * emitted over 20 msec ago.
  3305. *
  3306. * Note that if we were to use the current jiffies each time around the loop,
  3307. * we wouldn't escape the function with any frames outstanding if the time to
  3308. * render a frame was over 20ms.
  3309. *
  3310. * This should get us reasonable parallelism between CPU and GPU but also
  3311. * relatively low latency when blocking on a particular request to finish.
  3312. */
  3313. static int
  3314. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3315. {
  3316. struct drm_i915_private *dev_priv = to_i915(dev);
  3317. struct drm_i915_file_private *file_priv = file->driver_priv;
  3318. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3319. struct drm_i915_gem_request *request, *target = NULL;
  3320. long ret;
  3321. /* ABI: return -EIO if already wedged */
  3322. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3323. return -EIO;
  3324. spin_lock(&file_priv->mm.lock);
  3325. list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
  3326. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3327. break;
  3328. if (target) {
  3329. list_del(&target->client_link);
  3330. target->file_priv = NULL;
  3331. }
  3332. target = request;
  3333. }
  3334. if (target)
  3335. i915_gem_request_get(target);
  3336. spin_unlock(&file_priv->mm.lock);
  3337. if (target == NULL)
  3338. return 0;
  3339. ret = i915_wait_request(target,
  3340. I915_WAIT_INTERRUPTIBLE,
  3341. MAX_SCHEDULE_TIMEOUT);
  3342. i915_gem_request_put(target);
  3343. return ret < 0 ? ret : 0;
  3344. }
  3345. struct i915_vma *
  3346. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3347. const struct i915_ggtt_view *view,
  3348. u64 size,
  3349. u64 alignment,
  3350. u64 flags)
  3351. {
  3352. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  3353. struct i915_address_space *vm = &dev_priv->ggtt.base;
  3354. struct i915_vma *vma;
  3355. int ret;
  3356. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3357. vma = i915_vma_instance(obj, vm, view);
  3358. if (unlikely(IS_ERR(vma)))
  3359. return vma;
  3360. if (i915_vma_misplaced(vma, size, alignment, flags)) {
  3361. if (flags & PIN_NONBLOCK &&
  3362. (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
  3363. return ERR_PTR(-ENOSPC);
  3364. if (flags & PIN_MAPPABLE) {
  3365. /* If the required space is larger than the available
  3366. * aperture, we will not able to find a slot for the
  3367. * object and unbinding the object now will be in
  3368. * vain. Worse, doing so may cause us to ping-pong
  3369. * the object in and out of the Global GTT and
  3370. * waste a lot of cycles under the mutex.
  3371. */
  3372. if (vma->fence_size > dev_priv->ggtt.mappable_end)
  3373. return ERR_PTR(-E2BIG);
  3374. /* If NONBLOCK is set the caller is optimistically
  3375. * trying to cache the full object within the mappable
  3376. * aperture, and *must* have a fallback in place for
  3377. * situations where we cannot bind the object. We
  3378. * can be a little more lax here and use the fallback
  3379. * more often to avoid costly migrations of ourselves
  3380. * and other objects within the aperture.
  3381. *
  3382. * Half-the-aperture is used as a simple heuristic.
  3383. * More interesting would to do search for a free
  3384. * block prior to making the commitment to unbind.
  3385. * That caters for the self-harm case, and with a
  3386. * little more heuristics (e.g. NOFAULT, NOEVICT)
  3387. * we could try to minimise harm to others.
  3388. */
  3389. if (flags & PIN_NONBLOCK &&
  3390. vma->fence_size > dev_priv->ggtt.mappable_end / 2)
  3391. return ERR_PTR(-ENOSPC);
  3392. }
  3393. WARN(i915_vma_is_pinned(vma),
  3394. "bo is already pinned in ggtt with incorrect alignment:"
  3395. " offset=%08x, req.alignment=%llx,"
  3396. " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
  3397. i915_ggtt_offset(vma), alignment,
  3398. !!(flags & PIN_MAPPABLE),
  3399. i915_vma_is_map_and_fenceable(vma));
  3400. ret = i915_vma_unbind(vma);
  3401. if (ret)
  3402. return ERR_PTR(ret);
  3403. }
  3404. ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
  3405. if (ret)
  3406. return ERR_PTR(ret);
  3407. return vma;
  3408. }
  3409. static __always_inline unsigned int __busy_read_flag(unsigned int id)
  3410. {
  3411. /* Note that we could alias engines in the execbuf API, but
  3412. * that would be very unwise as it prevents userspace from
  3413. * fine control over engine selection. Ahem.
  3414. *
  3415. * This should be something like EXEC_MAX_ENGINE instead of
  3416. * I915_NUM_ENGINES.
  3417. */
  3418. BUILD_BUG_ON(I915_NUM_ENGINES > 16);
  3419. return 0x10000 << id;
  3420. }
  3421. static __always_inline unsigned int __busy_write_id(unsigned int id)
  3422. {
  3423. /* The uABI guarantees an active writer is also amongst the read
  3424. * engines. This would be true if we accessed the activity tracking
  3425. * under the lock, but as we perform the lookup of the object and
  3426. * its activity locklessly we can not guarantee that the last_write
  3427. * being active implies that we have set the same engine flag from
  3428. * last_read - hence we always set both read and write busy for
  3429. * last_write.
  3430. */
  3431. return id | __busy_read_flag(id);
  3432. }
  3433. static __always_inline unsigned int
  3434. __busy_set_if_active(const struct dma_fence *fence,
  3435. unsigned int (*flag)(unsigned int id))
  3436. {
  3437. struct drm_i915_gem_request *rq;
  3438. /* We have to check the current hw status of the fence as the uABI
  3439. * guarantees forward progress. We could rely on the idle worker
  3440. * to eventually flush us, but to minimise latency just ask the
  3441. * hardware.
  3442. *
  3443. * Note we only report on the status of native fences.
  3444. */
  3445. if (!dma_fence_is_i915(fence))
  3446. return 0;
  3447. /* opencode to_request() in order to avoid const warnings */
  3448. rq = container_of(fence, struct drm_i915_gem_request, fence);
  3449. if (i915_gem_request_completed(rq))
  3450. return 0;
  3451. return flag(rq->engine->uabi_id);
  3452. }
  3453. static __always_inline unsigned int
  3454. busy_check_reader(const struct dma_fence *fence)
  3455. {
  3456. return __busy_set_if_active(fence, __busy_read_flag);
  3457. }
  3458. static __always_inline unsigned int
  3459. busy_check_writer(const struct dma_fence *fence)
  3460. {
  3461. if (!fence)
  3462. return 0;
  3463. return __busy_set_if_active(fence, __busy_write_id);
  3464. }
  3465. int
  3466. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3467. struct drm_file *file)
  3468. {
  3469. struct drm_i915_gem_busy *args = data;
  3470. struct drm_i915_gem_object *obj;
  3471. struct reservation_object_list *list;
  3472. unsigned int seq;
  3473. int err;
  3474. err = -ENOENT;
  3475. rcu_read_lock();
  3476. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3477. if (!obj)
  3478. goto out;
  3479. /* A discrepancy here is that we do not report the status of
  3480. * non-i915 fences, i.e. even though we may report the object as idle,
  3481. * a call to set-domain may still stall waiting for foreign rendering.
  3482. * This also means that wait-ioctl may report an object as busy,
  3483. * where busy-ioctl considers it idle.
  3484. *
  3485. * We trade the ability to warn of foreign fences to report on which
  3486. * i915 engines are active for the object.
  3487. *
  3488. * Alternatively, we can trade that extra information on read/write
  3489. * activity with
  3490. * args->busy =
  3491. * !reservation_object_test_signaled_rcu(obj->resv, true);
  3492. * to report the overall busyness. This is what the wait-ioctl does.
  3493. *
  3494. */
  3495. retry:
  3496. seq = raw_read_seqcount(&obj->resv->seq);
  3497. /* Translate the exclusive fence to the READ *and* WRITE engine */
  3498. args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
  3499. /* Translate shared fences to READ set of engines */
  3500. list = rcu_dereference(obj->resv->fence);
  3501. if (list) {
  3502. unsigned int shared_count = list->shared_count, i;
  3503. for (i = 0; i < shared_count; ++i) {
  3504. struct dma_fence *fence =
  3505. rcu_dereference(list->shared[i]);
  3506. args->busy |= busy_check_reader(fence);
  3507. }
  3508. }
  3509. if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
  3510. goto retry;
  3511. err = 0;
  3512. out:
  3513. rcu_read_unlock();
  3514. return err;
  3515. }
  3516. int
  3517. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3518. struct drm_file *file_priv)
  3519. {
  3520. return i915_gem_ring_throttle(dev, file_priv);
  3521. }
  3522. int
  3523. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3524. struct drm_file *file_priv)
  3525. {
  3526. struct drm_i915_private *dev_priv = to_i915(dev);
  3527. struct drm_i915_gem_madvise *args = data;
  3528. struct drm_i915_gem_object *obj;
  3529. int err;
  3530. switch (args->madv) {
  3531. case I915_MADV_DONTNEED:
  3532. case I915_MADV_WILLNEED:
  3533. break;
  3534. default:
  3535. return -EINVAL;
  3536. }
  3537. obj = i915_gem_object_lookup(file_priv, args->handle);
  3538. if (!obj)
  3539. return -ENOENT;
  3540. err = mutex_lock_interruptible(&obj->mm.lock);
  3541. if (err)
  3542. goto out;
  3543. if (obj->mm.pages &&
  3544. i915_gem_object_is_tiled(obj) &&
  3545. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3546. if (obj->mm.madv == I915_MADV_WILLNEED) {
  3547. GEM_BUG_ON(!obj->mm.quirked);
  3548. __i915_gem_object_unpin_pages(obj);
  3549. obj->mm.quirked = false;
  3550. }
  3551. if (args->madv == I915_MADV_WILLNEED) {
  3552. GEM_BUG_ON(obj->mm.quirked);
  3553. __i915_gem_object_pin_pages(obj);
  3554. obj->mm.quirked = true;
  3555. }
  3556. }
  3557. if (obj->mm.madv != __I915_MADV_PURGED)
  3558. obj->mm.madv = args->madv;
  3559. /* if the object is no longer attached, discard its backing storage */
  3560. if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
  3561. i915_gem_object_truncate(obj);
  3562. args->retained = obj->mm.madv != __I915_MADV_PURGED;
  3563. mutex_unlock(&obj->mm.lock);
  3564. out:
  3565. i915_gem_object_put(obj);
  3566. return err;
  3567. }
  3568. static void
  3569. frontbuffer_retire(struct i915_gem_active *active,
  3570. struct drm_i915_gem_request *request)
  3571. {
  3572. struct drm_i915_gem_object *obj =
  3573. container_of(active, typeof(*obj), frontbuffer_write);
  3574. intel_fb_obj_flush(obj, ORIGIN_CS);
  3575. }
  3576. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3577. const struct drm_i915_gem_object_ops *ops)
  3578. {
  3579. mutex_init(&obj->mm.lock);
  3580. INIT_LIST_HEAD(&obj->global_link);
  3581. INIT_LIST_HEAD(&obj->userfault_link);
  3582. INIT_LIST_HEAD(&obj->vma_list);
  3583. INIT_LIST_HEAD(&obj->batch_pool_link);
  3584. obj->ops = ops;
  3585. reservation_object_init(&obj->__builtin_resv);
  3586. obj->resv = &obj->__builtin_resv;
  3587. obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
  3588. init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
  3589. obj->mm.madv = I915_MADV_WILLNEED;
  3590. INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
  3591. mutex_init(&obj->mm.get_page.lock);
  3592. i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
  3593. }
  3594. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3595. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
  3596. I915_GEM_OBJECT_IS_SHRINKABLE,
  3597. .get_pages = i915_gem_object_get_pages_gtt,
  3598. .put_pages = i915_gem_object_put_pages_gtt,
  3599. .pwrite = i915_gem_object_pwrite_gtt,
  3600. };
  3601. struct drm_i915_gem_object *
  3602. i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
  3603. {
  3604. struct drm_i915_gem_object *obj;
  3605. struct address_space *mapping;
  3606. gfp_t mask;
  3607. int ret;
  3608. /* There is a prevalence of the assumption that we fit the object's
  3609. * page count inside a 32bit _signed_ variable. Let's document this and
  3610. * catch if we ever need to fix it. In the meantime, if you do spot
  3611. * such a local variable, please consider fixing!
  3612. */
  3613. if (size >> PAGE_SHIFT > INT_MAX)
  3614. return ERR_PTR(-E2BIG);
  3615. if (overflows_type(size, obj->base.size))
  3616. return ERR_PTR(-E2BIG);
  3617. obj = i915_gem_object_alloc(dev_priv);
  3618. if (obj == NULL)
  3619. return ERR_PTR(-ENOMEM);
  3620. ret = drm_gem_object_init(&dev_priv->drm, &obj->base, size);
  3621. if (ret)
  3622. goto fail;
  3623. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3624. if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
  3625. /* 965gm cannot relocate objects above 4GiB. */
  3626. mask &= ~__GFP_HIGHMEM;
  3627. mask |= __GFP_DMA32;
  3628. }
  3629. mapping = obj->base.filp->f_mapping;
  3630. mapping_set_gfp_mask(mapping, mask);
  3631. GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
  3632. i915_gem_object_init(obj, &i915_gem_object_ops);
  3633. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3634. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3635. if (HAS_LLC(dev_priv)) {
  3636. /* On some devices, we can have the GPU use the LLC (the CPU
  3637. * cache) for about a 10% performance improvement
  3638. * compared to uncached. Graphics requests other than
  3639. * display scanout are coherent with the CPU in
  3640. * accessing this cache. This means in this mode we
  3641. * don't need to clflush on the CPU side, and on the
  3642. * GPU side we only need to flush internal caches to
  3643. * get data visible to the CPU.
  3644. *
  3645. * However, we maintain the display planes as UC, and so
  3646. * need to rebind when first used as such.
  3647. */
  3648. obj->cache_level = I915_CACHE_LLC;
  3649. } else
  3650. obj->cache_level = I915_CACHE_NONE;
  3651. obj->cache_coherent = i915_gem_object_is_coherent(obj);
  3652. obj->cache_dirty = !obj->cache_coherent;
  3653. trace_i915_gem_object_create(obj);
  3654. return obj;
  3655. fail:
  3656. i915_gem_object_free(obj);
  3657. return ERR_PTR(ret);
  3658. }
  3659. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3660. {
  3661. /* If we are the last user of the backing storage (be it shmemfs
  3662. * pages or stolen etc), we know that the pages are going to be
  3663. * immediately released. In this case, we can then skip copying
  3664. * back the contents from the GPU.
  3665. */
  3666. if (obj->mm.madv != I915_MADV_WILLNEED)
  3667. return false;
  3668. if (obj->base.filp == NULL)
  3669. return true;
  3670. /* At first glance, this looks racy, but then again so would be
  3671. * userspace racing mmap against close. However, the first external
  3672. * reference to the filp can only be obtained through the
  3673. * i915_gem_mmap_ioctl() which safeguards us against the user
  3674. * acquiring such a reference whilst we are in the middle of
  3675. * freeing the object.
  3676. */
  3677. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3678. }
  3679. static void __i915_gem_free_objects(struct drm_i915_private *i915,
  3680. struct llist_node *freed)
  3681. {
  3682. struct drm_i915_gem_object *obj, *on;
  3683. mutex_lock(&i915->drm.struct_mutex);
  3684. intel_runtime_pm_get(i915);
  3685. llist_for_each_entry(obj, freed, freed) {
  3686. struct i915_vma *vma, *vn;
  3687. trace_i915_gem_object_destroy(obj);
  3688. GEM_BUG_ON(i915_gem_object_is_active(obj));
  3689. list_for_each_entry_safe(vma, vn,
  3690. &obj->vma_list, obj_link) {
  3691. GEM_BUG_ON(i915_vma_is_active(vma));
  3692. vma->flags &= ~I915_VMA_PIN_MASK;
  3693. i915_vma_close(vma);
  3694. }
  3695. GEM_BUG_ON(!list_empty(&obj->vma_list));
  3696. GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
  3697. list_del(&obj->global_link);
  3698. }
  3699. intel_runtime_pm_put(i915);
  3700. mutex_unlock(&i915->drm.struct_mutex);
  3701. cond_resched();
  3702. llist_for_each_entry_safe(obj, on, freed, freed) {
  3703. GEM_BUG_ON(obj->bind_count);
  3704. GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
  3705. if (obj->ops->release)
  3706. obj->ops->release(obj);
  3707. if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
  3708. atomic_set(&obj->mm.pages_pin_count, 0);
  3709. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  3710. GEM_BUG_ON(obj->mm.pages);
  3711. if (obj->base.import_attach)
  3712. drm_prime_gem_destroy(&obj->base, NULL);
  3713. reservation_object_fini(&obj->__builtin_resv);
  3714. drm_gem_object_release(&obj->base);
  3715. i915_gem_info_remove_obj(i915, obj->base.size);
  3716. kfree(obj->bit_17);
  3717. i915_gem_object_free(obj);
  3718. }
  3719. }
  3720. static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
  3721. {
  3722. struct llist_node *freed;
  3723. freed = llist_del_all(&i915->mm.free_list);
  3724. if (unlikely(freed))
  3725. __i915_gem_free_objects(i915, freed);
  3726. }
  3727. static void __i915_gem_free_work(struct work_struct *work)
  3728. {
  3729. struct drm_i915_private *i915 =
  3730. container_of(work, struct drm_i915_private, mm.free_work);
  3731. struct llist_node *freed;
  3732. /* All file-owned VMA should have been released by this point through
  3733. * i915_gem_close_object(), or earlier by i915_gem_context_close().
  3734. * However, the object may also be bound into the global GTT (e.g.
  3735. * older GPUs without per-process support, or for direct access through
  3736. * the GTT either for the user or for scanout). Those VMA still need to
  3737. * unbound now.
  3738. */
  3739. while ((freed = llist_del_all(&i915->mm.free_list))) {
  3740. __i915_gem_free_objects(i915, freed);
  3741. if (need_resched())
  3742. break;
  3743. }
  3744. }
  3745. static void __i915_gem_free_object_rcu(struct rcu_head *head)
  3746. {
  3747. struct drm_i915_gem_object *obj =
  3748. container_of(head, typeof(*obj), rcu);
  3749. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3750. /* We can't simply use call_rcu() from i915_gem_free_object()
  3751. * as we need to block whilst unbinding, and the call_rcu
  3752. * task may be called from softirq context. So we take a
  3753. * detour through a worker.
  3754. */
  3755. if (llist_add(&obj->freed, &i915->mm.free_list))
  3756. schedule_work(&i915->mm.free_work);
  3757. }
  3758. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3759. {
  3760. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3761. if (obj->mm.quirked)
  3762. __i915_gem_object_unpin_pages(obj);
  3763. if (discard_backing_storage(obj))
  3764. obj->mm.madv = I915_MADV_DONTNEED;
  3765. /* Before we free the object, make sure any pure RCU-only
  3766. * read-side critical sections are complete, e.g.
  3767. * i915_gem_busy_ioctl(). For the corresponding synchronized
  3768. * lookup see i915_gem_object_lookup_rcu().
  3769. */
  3770. call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
  3771. }
  3772. void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
  3773. {
  3774. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3775. GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
  3776. if (i915_gem_object_is_active(obj))
  3777. i915_gem_object_set_active_reference(obj);
  3778. else
  3779. i915_gem_object_put(obj);
  3780. }
  3781. static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
  3782. {
  3783. struct intel_engine_cs *engine;
  3784. enum intel_engine_id id;
  3785. for_each_engine(engine, dev_priv, id)
  3786. GEM_BUG_ON(engine->last_retired_context &&
  3787. !i915_gem_context_is_kernel(engine->last_retired_context));
  3788. }
  3789. void i915_gem_sanitize(struct drm_i915_private *i915)
  3790. {
  3791. /*
  3792. * If we inherit context state from the BIOS or earlier occupants
  3793. * of the GPU, the GPU may be in an inconsistent state when we
  3794. * try to take over. The only way to remove the earlier state
  3795. * is by resetting. However, resetting on earlier gen is tricky as
  3796. * it may impact the display and we are uncertain about the stability
  3797. * of the reset, so this could be applied to even earlier gen.
  3798. */
  3799. if (INTEL_GEN(i915) >= 5) {
  3800. int reset = intel_gpu_reset(i915, ALL_ENGINES);
  3801. WARN_ON(reset && reset != -ENODEV);
  3802. }
  3803. }
  3804. int i915_gem_suspend(struct drm_i915_private *dev_priv)
  3805. {
  3806. struct drm_device *dev = &dev_priv->drm;
  3807. int ret;
  3808. intel_runtime_pm_get(dev_priv);
  3809. intel_suspend_gt_powersave(dev_priv);
  3810. mutex_lock(&dev->struct_mutex);
  3811. /* We have to flush all the executing contexts to main memory so
  3812. * that they can saved in the hibernation image. To ensure the last
  3813. * context image is coherent, we have to switch away from it. That
  3814. * leaves the dev_priv->kernel_context still active when
  3815. * we actually suspend, and its image in memory may not match the GPU
  3816. * state. Fortunately, the kernel_context is disposable and we do
  3817. * not rely on its state.
  3818. */
  3819. ret = i915_gem_switch_to_kernel_context(dev_priv);
  3820. if (ret)
  3821. goto err_unlock;
  3822. ret = i915_gem_wait_for_idle(dev_priv,
  3823. I915_WAIT_INTERRUPTIBLE |
  3824. I915_WAIT_LOCKED);
  3825. if (ret)
  3826. goto err_unlock;
  3827. assert_kernel_context_is_current(dev_priv);
  3828. i915_gem_contexts_lost(dev_priv);
  3829. mutex_unlock(&dev->struct_mutex);
  3830. intel_guc_suspend(dev_priv);
  3831. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  3832. cancel_delayed_work_sync(&dev_priv->gt.retire_work);
  3833. /* As the idle_work is rearming if it detects a race, play safe and
  3834. * repeat the flush until it is definitely idle.
  3835. */
  3836. while (flush_delayed_work(&dev_priv->gt.idle_work))
  3837. ;
  3838. /* Assert that we sucessfully flushed all the work and
  3839. * reset the GPU back to its idle, low power state.
  3840. */
  3841. WARN_ON(dev_priv->gt.awake);
  3842. WARN_ON(!intel_engines_are_idle(dev_priv));
  3843. /*
  3844. * Neither the BIOS, ourselves or any other kernel
  3845. * expects the system to be in execlists mode on startup,
  3846. * so we need to reset the GPU back to legacy mode. And the only
  3847. * known way to disable logical contexts is through a GPU reset.
  3848. *
  3849. * So in order to leave the system in a known default configuration,
  3850. * always reset the GPU upon unload and suspend. Afterwards we then
  3851. * clean up the GEM state tracking, flushing off the requests and
  3852. * leaving the system in a known idle state.
  3853. *
  3854. * Note that is of the upmost importance that the GPU is idle and
  3855. * all stray writes are flushed *before* we dismantle the backing
  3856. * storage for the pinned objects.
  3857. *
  3858. * However, since we are uncertain that resetting the GPU on older
  3859. * machines is a good idea, we don't - just in case it leaves the
  3860. * machine in an unusable condition.
  3861. */
  3862. i915_gem_sanitize(dev_priv);
  3863. goto out_rpm_put;
  3864. err_unlock:
  3865. mutex_unlock(&dev->struct_mutex);
  3866. out_rpm_put:
  3867. intel_runtime_pm_put(dev_priv);
  3868. return ret;
  3869. }
  3870. void i915_gem_resume(struct drm_i915_private *dev_priv)
  3871. {
  3872. struct drm_device *dev = &dev_priv->drm;
  3873. WARN_ON(dev_priv->gt.awake);
  3874. mutex_lock(&dev->struct_mutex);
  3875. i915_gem_restore_gtt_mappings(dev_priv);
  3876. /* As we didn't flush the kernel context before suspend, we cannot
  3877. * guarantee that the context image is complete. So let's just reset
  3878. * it and start again.
  3879. */
  3880. dev_priv->gt.resume(dev_priv);
  3881. mutex_unlock(&dev->struct_mutex);
  3882. }
  3883. void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
  3884. {
  3885. if (INTEL_GEN(dev_priv) < 5 ||
  3886. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3887. return;
  3888. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3889. DISP_TILE_SURFACE_SWIZZLING);
  3890. if (IS_GEN5(dev_priv))
  3891. return;
  3892. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3893. if (IS_GEN6(dev_priv))
  3894. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3895. else if (IS_GEN7(dev_priv))
  3896. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3897. else if (IS_GEN8(dev_priv))
  3898. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3899. else
  3900. BUG();
  3901. }
  3902. static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
  3903. {
  3904. I915_WRITE(RING_CTL(base), 0);
  3905. I915_WRITE(RING_HEAD(base), 0);
  3906. I915_WRITE(RING_TAIL(base), 0);
  3907. I915_WRITE(RING_START(base), 0);
  3908. }
  3909. static void init_unused_rings(struct drm_i915_private *dev_priv)
  3910. {
  3911. if (IS_I830(dev_priv)) {
  3912. init_unused_ring(dev_priv, PRB1_BASE);
  3913. init_unused_ring(dev_priv, SRB0_BASE);
  3914. init_unused_ring(dev_priv, SRB1_BASE);
  3915. init_unused_ring(dev_priv, SRB2_BASE);
  3916. init_unused_ring(dev_priv, SRB3_BASE);
  3917. } else if (IS_GEN2(dev_priv)) {
  3918. init_unused_ring(dev_priv, SRB0_BASE);
  3919. init_unused_ring(dev_priv, SRB1_BASE);
  3920. } else if (IS_GEN3(dev_priv)) {
  3921. init_unused_ring(dev_priv, PRB1_BASE);
  3922. init_unused_ring(dev_priv, PRB2_BASE);
  3923. }
  3924. }
  3925. static int __i915_gem_restart_engines(void *data)
  3926. {
  3927. struct drm_i915_private *i915 = data;
  3928. struct intel_engine_cs *engine;
  3929. enum intel_engine_id id;
  3930. int err;
  3931. for_each_engine(engine, i915, id) {
  3932. err = engine->init_hw(engine);
  3933. if (err)
  3934. return err;
  3935. }
  3936. return 0;
  3937. }
  3938. int i915_gem_init_hw(struct drm_i915_private *dev_priv)
  3939. {
  3940. int ret;
  3941. dev_priv->gt.last_init_time = ktime_get();
  3942. /* Double layer security blanket, see i915_gem_init() */
  3943. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3944. if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
  3945. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3946. if (IS_HASWELL(dev_priv))
  3947. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
  3948. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3949. if (HAS_PCH_NOP(dev_priv)) {
  3950. if (IS_IVYBRIDGE(dev_priv)) {
  3951. u32 temp = I915_READ(GEN7_MSG_CTL);
  3952. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3953. I915_WRITE(GEN7_MSG_CTL, temp);
  3954. } else if (INTEL_GEN(dev_priv) >= 7) {
  3955. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3956. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3957. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3958. }
  3959. }
  3960. i915_gem_init_swizzling(dev_priv);
  3961. /*
  3962. * At least 830 can leave some of the unused rings
  3963. * "active" (ie. head != tail) after resume which
  3964. * will prevent c3 entry. Makes sure all unused rings
  3965. * are totally idle.
  3966. */
  3967. init_unused_rings(dev_priv);
  3968. BUG_ON(!dev_priv->kernel_context);
  3969. ret = i915_ppgtt_init_hw(dev_priv);
  3970. if (ret) {
  3971. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  3972. goto out;
  3973. }
  3974. /* Need to do basic initialisation of all rings first: */
  3975. ret = __i915_gem_restart_engines(dev_priv);
  3976. if (ret)
  3977. goto out;
  3978. intel_mocs_init_l3cc_table(dev_priv);
  3979. /* We can't enable contexts until all firmware is loaded */
  3980. ret = intel_uc_init_hw(dev_priv);
  3981. if (ret)
  3982. goto out;
  3983. out:
  3984. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3985. return ret;
  3986. }
  3987. bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
  3988. {
  3989. if (INTEL_INFO(dev_priv)->gen < 6)
  3990. return false;
  3991. /* TODO: make semaphores and Execlists play nicely together */
  3992. if (i915.enable_execlists)
  3993. return false;
  3994. if (value >= 0)
  3995. return value;
  3996. /* Enable semaphores on SNB when IO remapping is off */
  3997. if (IS_GEN6(dev_priv) && intel_vtd_active())
  3998. return false;
  3999. return true;
  4000. }
  4001. int i915_gem_init(struct drm_i915_private *dev_priv)
  4002. {
  4003. int ret;
  4004. mutex_lock(&dev_priv->drm.struct_mutex);
  4005. dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
  4006. if (!i915.enable_execlists) {
  4007. dev_priv->gt.resume = intel_legacy_submission_resume;
  4008. dev_priv->gt.cleanup_engine = intel_engine_cleanup;
  4009. } else {
  4010. dev_priv->gt.resume = intel_lr_context_resume;
  4011. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  4012. }
  4013. /* This is just a security blanket to placate dragons.
  4014. * On some systems, we very sporadically observe that the first TLBs
  4015. * used by the CS may be stale, despite us poking the TLB reset. If
  4016. * we hold the forcewake during initialisation these problems
  4017. * just magically go away.
  4018. */
  4019. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4020. ret = i915_gem_init_userptr(dev_priv);
  4021. if (ret)
  4022. goto out_unlock;
  4023. ret = i915_gem_init_ggtt(dev_priv);
  4024. if (ret)
  4025. goto out_unlock;
  4026. ret = i915_gem_contexts_init(dev_priv);
  4027. if (ret)
  4028. goto out_unlock;
  4029. ret = intel_engines_init(dev_priv);
  4030. if (ret)
  4031. goto out_unlock;
  4032. ret = i915_gem_init_hw(dev_priv);
  4033. if (ret == -EIO) {
  4034. /* Allow engine initialisation to fail by marking the GPU as
  4035. * wedged. But we only want to do this where the GPU is angry,
  4036. * for all other failure, such as an allocation failure, bail.
  4037. */
  4038. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4039. i915_gem_set_wedged(dev_priv);
  4040. ret = 0;
  4041. }
  4042. out_unlock:
  4043. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4044. mutex_unlock(&dev_priv->drm.struct_mutex);
  4045. return ret;
  4046. }
  4047. void i915_gem_init_mmio(struct drm_i915_private *i915)
  4048. {
  4049. i915_gem_sanitize(i915);
  4050. }
  4051. void
  4052. i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
  4053. {
  4054. struct intel_engine_cs *engine;
  4055. enum intel_engine_id id;
  4056. for_each_engine(engine, dev_priv, id)
  4057. dev_priv->gt.cleanup_engine(engine);
  4058. }
  4059. void
  4060. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  4061. {
  4062. int i;
  4063. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  4064. !IS_CHERRYVIEW(dev_priv))
  4065. dev_priv->num_fence_regs = 32;
  4066. else if (INTEL_INFO(dev_priv)->gen >= 4 ||
  4067. IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  4068. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
  4069. dev_priv->num_fence_regs = 16;
  4070. else
  4071. dev_priv->num_fence_regs = 8;
  4072. if (intel_vgpu_active(dev_priv))
  4073. dev_priv->num_fence_regs =
  4074. I915_READ(vgtif_reg(avail_rs.fence_num));
  4075. /* Initialize fence registers to zero */
  4076. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  4077. struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
  4078. fence->i915 = dev_priv;
  4079. fence->id = i;
  4080. list_add_tail(&fence->link, &dev_priv->mm.fence_list);
  4081. }
  4082. i915_gem_restore_fences(dev_priv);
  4083. i915_gem_detect_bit_6_swizzle(dev_priv);
  4084. }
  4085. int
  4086. i915_gem_load_init(struct drm_i915_private *dev_priv)
  4087. {
  4088. int err = -ENOMEM;
  4089. dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
  4090. if (!dev_priv->objects)
  4091. goto err_out;
  4092. dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
  4093. if (!dev_priv->vmas)
  4094. goto err_objects;
  4095. dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
  4096. SLAB_HWCACHE_ALIGN |
  4097. SLAB_RECLAIM_ACCOUNT |
  4098. SLAB_TYPESAFE_BY_RCU);
  4099. if (!dev_priv->requests)
  4100. goto err_vmas;
  4101. dev_priv->dependencies = KMEM_CACHE(i915_dependency,
  4102. SLAB_HWCACHE_ALIGN |
  4103. SLAB_RECLAIM_ACCOUNT);
  4104. if (!dev_priv->dependencies)
  4105. goto err_requests;
  4106. dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
  4107. if (!dev_priv->priorities)
  4108. goto err_dependencies;
  4109. mutex_lock(&dev_priv->drm.struct_mutex);
  4110. INIT_LIST_HEAD(&dev_priv->gt.timelines);
  4111. err = i915_gem_timeline_init__global(dev_priv);
  4112. mutex_unlock(&dev_priv->drm.struct_mutex);
  4113. if (err)
  4114. goto err_priorities;
  4115. INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
  4116. init_llist_head(&dev_priv->mm.free_list);
  4117. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4118. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4119. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4120. INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
  4121. INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
  4122. i915_gem_retire_work_handler);
  4123. INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
  4124. i915_gem_idle_work_handler);
  4125. init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
  4126. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4127. atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
  4128. spin_lock_init(&dev_priv->fb_tracking.lock);
  4129. return 0;
  4130. err_priorities:
  4131. kmem_cache_destroy(dev_priv->priorities);
  4132. err_dependencies:
  4133. kmem_cache_destroy(dev_priv->dependencies);
  4134. err_requests:
  4135. kmem_cache_destroy(dev_priv->requests);
  4136. err_vmas:
  4137. kmem_cache_destroy(dev_priv->vmas);
  4138. err_objects:
  4139. kmem_cache_destroy(dev_priv->objects);
  4140. err_out:
  4141. return err;
  4142. }
  4143. void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
  4144. {
  4145. i915_gem_drain_freed_objects(dev_priv);
  4146. WARN_ON(!llist_empty(&dev_priv->mm.free_list));
  4147. WARN_ON(dev_priv->mm.object_count);
  4148. mutex_lock(&dev_priv->drm.struct_mutex);
  4149. i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
  4150. WARN_ON(!list_empty(&dev_priv->gt.timelines));
  4151. mutex_unlock(&dev_priv->drm.struct_mutex);
  4152. kmem_cache_destroy(dev_priv->priorities);
  4153. kmem_cache_destroy(dev_priv->dependencies);
  4154. kmem_cache_destroy(dev_priv->requests);
  4155. kmem_cache_destroy(dev_priv->vmas);
  4156. kmem_cache_destroy(dev_priv->objects);
  4157. /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
  4158. rcu_barrier();
  4159. }
  4160. int i915_gem_freeze(struct drm_i915_private *dev_priv)
  4161. {
  4162. /* Discard all purgeable objects, let userspace recover those as
  4163. * required after resuming.
  4164. */
  4165. i915_gem_shrink_all(dev_priv);
  4166. return 0;
  4167. }
  4168. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  4169. {
  4170. struct drm_i915_gem_object *obj;
  4171. struct list_head *phases[] = {
  4172. &dev_priv->mm.unbound_list,
  4173. &dev_priv->mm.bound_list,
  4174. NULL
  4175. }, **p;
  4176. /* Called just before we write the hibernation image.
  4177. *
  4178. * We need to update the domain tracking to reflect that the CPU
  4179. * will be accessing all the pages to create and restore from the
  4180. * hibernation, and so upon restoration those pages will be in the
  4181. * CPU domain.
  4182. *
  4183. * To make sure the hibernation image contains the latest state,
  4184. * we update that state just before writing out the image.
  4185. *
  4186. * To try and reduce the hibernation image, we manually shrink
  4187. * the objects as well, see i915_gem_freeze()
  4188. */
  4189. i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
  4190. i915_gem_drain_freed_objects(dev_priv);
  4191. mutex_lock(&dev_priv->drm.struct_mutex);
  4192. for (p = phases; *p; p++) {
  4193. list_for_each_entry(obj, *p, global_link)
  4194. __start_cpu_write(obj);
  4195. }
  4196. mutex_unlock(&dev_priv->drm.struct_mutex);
  4197. return 0;
  4198. }
  4199. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4200. {
  4201. struct drm_i915_file_private *file_priv = file->driver_priv;
  4202. struct drm_i915_gem_request *request;
  4203. /* Clean up our request list when the client is going away, so that
  4204. * later retire_requests won't dereference our soon-to-be-gone
  4205. * file_priv.
  4206. */
  4207. spin_lock(&file_priv->mm.lock);
  4208. list_for_each_entry(request, &file_priv->mm.request_list, client_link)
  4209. request->file_priv = NULL;
  4210. spin_unlock(&file_priv->mm.lock);
  4211. }
  4212. int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
  4213. {
  4214. struct drm_i915_file_private *file_priv;
  4215. int ret;
  4216. DRM_DEBUG("\n");
  4217. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4218. if (!file_priv)
  4219. return -ENOMEM;
  4220. file->driver_priv = file_priv;
  4221. file_priv->dev_priv = i915;
  4222. file_priv->file = file;
  4223. spin_lock_init(&file_priv->mm.lock);
  4224. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4225. file_priv->bsd_engine = -1;
  4226. ret = i915_gem_context_open(i915, file);
  4227. if (ret)
  4228. kfree(file_priv);
  4229. return ret;
  4230. }
  4231. /**
  4232. * i915_gem_track_fb - update frontbuffer tracking
  4233. * @old: current GEM buffer for the frontbuffer slots
  4234. * @new: new GEM buffer for the frontbuffer slots
  4235. * @frontbuffer_bits: bitmask of frontbuffer slots
  4236. *
  4237. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4238. * from @old and setting them in @new. Both @old and @new can be NULL.
  4239. */
  4240. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4241. struct drm_i915_gem_object *new,
  4242. unsigned frontbuffer_bits)
  4243. {
  4244. /* Control of individual bits within the mask are guarded by
  4245. * the owning plane->mutex, i.e. we can never see concurrent
  4246. * manipulation of individual bits. But since the bitfield as a whole
  4247. * is updated using RMW, we need to use atomics in order to update
  4248. * the bits.
  4249. */
  4250. BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
  4251. sizeof(atomic_t) * BITS_PER_BYTE);
  4252. if (old) {
  4253. WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
  4254. atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
  4255. }
  4256. if (new) {
  4257. WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
  4258. atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
  4259. }
  4260. }
  4261. /* Allocate a new GEM object and fill it with the supplied data */
  4262. struct drm_i915_gem_object *
  4263. i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
  4264. const void *data, size_t size)
  4265. {
  4266. struct drm_i915_gem_object *obj;
  4267. struct file *file;
  4268. size_t offset;
  4269. int err;
  4270. obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
  4271. if (IS_ERR(obj))
  4272. return obj;
  4273. GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
  4274. file = obj->base.filp;
  4275. offset = 0;
  4276. do {
  4277. unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
  4278. struct page *page;
  4279. void *pgdata, *vaddr;
  4280. err = pagecache_write_begin(file, file->f_mapping,
  4281. offset, len, 0,
  4282. &page, &pgdata);
  4283. if (err < 0)
  4284. goto fail;
  4285. vaddr = kmap(page);
  4286. memcpy(vaddr, data, len);
  4287. kunmap(page);
  4288. err = pagecache_write_end(file, file->f_mapping,
  4289. offset, len, len,
  4290. page, pgdata);
  4291. if (err < 0)
  4292. goto fail;
  4293. size -= len;
  4294. data += len;
  4295. offset += len;
  4296. } while (size);
  4297. return obj;
  4298. fail:
  4299. i915_gem_object_put(obj);
  4300. return ERR_PTR(err);
  4301. }
  4302. struct scatterlist *
  4303. i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
  4304. unsigned int n,
  4305. unsigned int *offset)
  4306. {
  4307. struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
  4308. struct scatterlist *sg;
  4309. unsigned int idx, count;
  4310. might_sleep();
  4311. GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
  4312. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
  4313. /* As we iterate forward through the sg, we record each entry in a
  4314. * radixtree for quick repeated (backwards) lookups. If we have seen
  4315. * this index previously, we will have an entry for it.
  4316. *
  4317. * Initial lookup is O(N), but this is amortized to O(1) for
  4318. * sequential page access (where each new request is consecutive
  4319. * to the previous one). Repeated lookups are O(lg(obj->base.size)),
  4320. * i.e. O(1) with a large constant!
  4321. */
  4322. if (n < READ_ONCE(iter->sg_idx))
  4323. goto lookup;
  4324. mutex_lock(&iter->lock);
  4325. /* We prefer to reuse the last sg so that repeated lookup of this
  4326. * (or the subsequent) sg are fast - comparing against the last
  4327. * sg is faster than going through the radixtree.
  4328. */
  4329. sg = iter->sg_pos;
  4330. idx = iter->sg_idx;
  4331. count = __sg_page_count(sg);
  4332. while (idx + count <= n) {
  4333. unsigned long exception, i;
  4334. int ret;
  4335. /* If we cannot allocate and insert this entry, or the
  4336. * individual pages from this range, cancel updating the
  4337. * sg_idx so that on this lookup we are forced to linearly
  4338. * scan onwards, but on future lookups we will try the
  4339. * insertion again (in which case we need to be careful of
  4340. * the error return reporting that we have already inserted
  4341. * this index).
  4342. */
  4343. ret = radix_tree_insert(&iter->radix, idx, sg);
  4344. if (ret && ret != -EEXIST)
  4345. goto scan;
  4346. exception =
  4347. RADIX_TREE_EXCEPTIONAL_ENTRY |
  4348. idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
  4349. for (i = 1; i < count; i++) {
  4350. ret = radix_tree_insert(&iter->radix, idx + i,
  4351. (void *)exception);
  4352. if (ret && ret != -EEXIST)
  4353. goto scan;
  4354. }
  4355. idx += count;
  4356. sg = ____sg_next(sg);
  4357. count = __sg_page_count(sg);
  4358. }
  4359. scan:
  4360. iter->sg_pos = sg;
  4361. iter->sg_idx = idx;
  4362. mutex_unlock(&iter->lock);
  4363. if (unlikely(n < idx)) /* insertion completed by another thread */
  4364. goto lookup;
  4365. /* In case we failed to insert the entry into the radixtree, we need
  4366. * to look beyond the current sg.
  4367. */
  4368. while (idx + count <= n) {
  4369. idx += count;
  4370. sg = ____sg_next(sg);
  4371. count = __sg_page_count(sg);
  4372. }
  4373. *offset = n - idx;
  4374. return sg;
  4375. lookup:
  4376. rcu_read_lock();
  4377. sg = radix_tree_lookup(&iter->radix, n);
  4378. GEM_BUG_ON(!sg);
  4379. /* If this index is in the middle of multi-page sg entry,
  4380. * the radixtree will contain an exceptional entry that points
  4381. * to the start of that range. We will return the pointer to
  4382. * the base page and the offset of this page within the
  4383. * sg entry's range.
  4384. */
  4385. *offset = 0;
  4386. if (unlikely(radix_tree_exception(sg))) {
  4387. unsigned long base =
  4388. (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
  4389. sg = radix_tree_lookup(&iter->radix, base);
  4390. GEM_BUG_ON(!sg);
  4391. *offset = n - base;
  4392. }
  4393. rcu_read_unlock();
  4394. return sg;
  4395. }
  4396. struct page *
  4397. i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
  4398. {
  4399. struct scatterlist *sg;
  4400. unsigned int offset;
  4401. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  4402. sg = i915_gem_object_get_sg(obj, n, &offset);
  4403. return nth_page(sg_page(sg), offset);
  4404. }
  4405. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  4406. struct page *
  4407. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
  4408. unsigned int n)
  4409. {
  4410. struct page *page;
  4411. page = i915_gem_object_get_page(obj, n);
  4412. if (!obj->mm.dirty)
  4413. set_page_dirty(page);
  4414. return page;
  4415. }
  4416. dma_addr_t
  4417. i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
  4418. unsigned long n)
  4419. {
  4420. struct scatterlist *sg;
  4421. unsigned int offset;
  4422. sg = i915_gem_object_get_sg(obj, n, &offset);
  4423. return sg_dma_address(sg) + (offset << PAGE_SHIFT);
  4424. }
  4425. int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
  4426. {
  4427. struct sg_table *pages;
  4428. int err;
  4429. if (align > obj->base.size)
  4430. return -EINVAL;
  4431. if (obj->ops == &i915_gem_phys_ops)
  4432. return 0;
  4433. if (obj->ops != &i915_gem_object_ops)
  4434. return -EINVAL;
  4435. err = i915_gem_object_unbind(obj);
  4436. if (err)
  4437. return err;
  4438. mutex_lock(&obj->mm.lock);
  4439. if (obj->mm.madv != I915_MADV_WILLNEED) {
  4440. err = -EFAULT;
  4441. goto err_unlock;
  4442. }
  4443. if (obj->mm.quirked) {
  4444. err = -EFAULT;
  4445. goto err_unlock;
  4446. }
  4447. if (obj->mm.mapping) {
  4448. err = -EBUSY;
  4449. goto err_unlock;
  4450. }
  4451. pages = obj->mm.pages;
  4452. obj->ops = &i915_gem_phys_ops;
  4453. err = ____i915_gem_object_get_pages(obj);
  4454. if (err)
  4455. goto err_xfer;
  4456. /* Perma-pin (until release) the physical set of pages */
  4457. __i915_gem_object_pin_pages(obj);
  4458. if (!IS_ERR_OR_NULL(pages))
  4459. i915_gem_object_ops.put_pages(obj, pages);
  4460. mutex_unlock(&obj->mm.lock);
  4461. return 0;
  4462. err_xfer:
  4463. obj->ops = &i915_gem_object_ops;
  4464. obj->mm.pages = pages;
  4465. err_unlock:
  4466. mutex_unlock(&obj->mm.lock);
  4467. return err;
  4468. }
  4469. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  4470. #include "selftests/scatterlist.c"
  4471. #include "selftests/mock_gem_device.c"
  4472. #include "selftests/huge_gem_object.c"
  4473. #include "selftests/i915_gem_object.c"
  4474. #include "selftests/i915_gem_coherency.c"
  4475. #endif